Hi all,
I'm write some custom widgets for my app.
Now i write custom Scale widget like in Gimp2.7.
Here is the code:
http://www.tbs-software.com/spookyln/py/Tk_Scale.py
but i need some kick how to make it editable.When i click to Canvas
focus set to editable entry.First value ok and next is error that focus
not set in item :(
BTW: is possible make gradient fill in Canvas.create_rectangle() ?
thanks for info
#encoding: utf-8
'''
Simple Scale widget like in Gimp beta
'''
from Tkinter import Tk, Canvas, Frame, SEL_FIRST, SEL_LAST
class GScale(Canvas):
__slots__ = ['value', '_max', '_min', 'text', 'objects']
def __init__(self, parent, text, _min=0, _max=255, relief='ridge'):
self.main = Frame(parent, bd=2, relief=relief)
Canvas.__init__(self, self.main)
Canvas.grid(self, column=0, row=0, sticky='nw')
self.value = 125
self._max = _max
self._min = _min
self.text = text
self.objects = {}
self.config(borderwidth=0, height=24, width=200,
selectborderwidth=0, highlightthickness=0)
self.bind('<Double-Button-1>', self.__select_value)
self.bind('<Button-1>', self.__repaint_widget)
self.bind('<B1-Motion>', self.__repaint_widget)
self.bind('<Key>', self.__edit_value)
def __recalc(self, pos):
rozsah = self._max - self._min
x = (pos * rozsah) / int(self['width'])
return int(x + self._min)
def __repaint_widget(self, event=None):
x = self.canvasx(event.x)
self.value = self.__recalc(x)
if self.value < self._min: self.value = self._min
elif self.value > self._max: self.value = self._max
for item in self.objects:
self.delete(self.objects[item])
fill = self.create_rectangle((0, 0, x, self['height']),
fill = 'cyan',
outline= 'cyan')
self.objects['rect'] = fill
label = self.create_text(2, 2, anchor='nw', text=self.text, font='Droid 9 bold')
self.objects['label'] = label
entry = self.create_text(self['width'], self['height'], anchor='se', text=str(self.value))
self.objects['entry'] = entry
def __select_value(self, event=None):
item = self.objects['entry']
self.focus_force()
self.focus(item)
self.select_from(item, 0)
self.select_to(item, 'end')
#x, y = map(int, self.coords(item))
#self.icursor(item, '@%d,%d' % (x, y))
def __edit_value(self, event=None):
item = self.objects['entry']
insert = self.index(item, 'insert')
if event.char in ['-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
self.dchars(item, SEL_FIRST, SEL_LAST)
self.select_clear()
self.insert(item, 'insert', event.char)
def grid(self, **kw):
self.main.grid(**kw)
def pack(self, **kw):
self.main.pack(**kw)
def place(self, **kw):
self.main.place(**kw)
if __name__ == '__main__':
app = Tk()
sli = GScale(app, 'Set attributes', _min=-134, _max=211)
sli.place(x=5, y=5)
app.mainloop()
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss