1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| try: from tkinter import *
except: from Tkinter import *
# Class TkComboBox
# ComboBox en pur Tkinter
#
# Options:
# width, height, border, background (bg) , foreground (fg), font, relief, cursor,
# exportselection, selectbackground, selectforeground
#
# Methodes:
# activate(int index)
# focus_set()
# focus()
# curselection()
# delete(ALL ou int)
# start (int)
# end (END ou int)
# insert(index, valeur)
# get()
# pack(padx=int, pady=int, fill(X, Y, BOTH), expand=bool, side=LEFT, RIGHT, TOP, BOTTOM, CENTER)
# grid(row=int, column=int, columnspan=int, rowspan=int, sticky=NSEW, padx=int, pady=int)
# place(relheight=int, relwidth=int, relx=int, rely=int, width=int, height=int, anchor=NSEW, x=int, y=int)
# config/configure(Option=Valeur)
# defaultvalue(int)
#
class TkComboBox(Toplevel):
listeobjets = []
# Positionnement
def widgetstate(self, event=None):
# True : Visible
# False : Cache la listbox
self.geometry('+%d+%d'%(self.zoneselection.winfo_rootx(),self.zoneselection.winfo_rooty()))
if self.valeur_widget_state == True:
self.valeur_widget_state = False
self.withdraw()
elif self.valeur_widget_state == False:
self.valeur_widget_state = True
self.deiconify()
def reposition(self, event):
self.valeur_widget_state = True
self.withdraw()
# Gestionnaires de geometrie
def pack(self, padx=None, pady=None, fill=None, expand=None, side=None):
self.zoneselection.pack(padx=padx, pady=pady, fill=fill, expand=expand, side=side)
def grid(self, row=None, column=None, columnspan=None, rowspan=None, sticky=None, padx=None, pady=None):
self.zoneselection.grid(row=row, column=column, columnspan=columnspan, rowspan=rowspan, sticky=sticky, padx=padx, pady=pady)
def place(self, relheight=None, relwidth=None, relx=None, rely=None, width=None, height=None, anchor=None, x=None, y=None):
self.zoneselection.place(relheight=relheight, relwidth=relwidth, relx=relx, rely=rely, width=width, height=height, anchor=anchor, x=x, y=y)
# Configuration
def configure(self, **kw):
for cle in kw:
option=cle
valeur=kw[cle]
self.zoneselection[option]=valeur
self.cache[option]=valeur
self.selection[option]=valeur
config=configure
# Methodes
def activate(self, index):
self.selection.activate(index)
def size(self):
return self.selection.size()
def focus(self):
self.zoneselection.get_focus()
def get_focus(self):
self.zoneselection.get_focus()
def curselection(self):
print map(int, self.selection.curselection())[0]
return map(int, self.selection.curselection())[0]
def delete(self, objet=None, start=None, end=None):
if objet=='ALL':
self.selection.delete(0, END)
elif start == None and end == None:
self.selection.delete(objet)
else:
self.selection.delete(start, end)
def get(self):
return self.zoneselection.get()
def insert(self, start, objet):
self.listeobjets.append(objet)
self.selection.insert(start, objet)
self.selection.select_set(0)
self.v_zoneselection.set(objet)
self.v_cache.set(objet)
def selectionner(self, event):
def index(event):
self.selection.curselection()[0]
self.v_zoneselection.set(self.selection.curselection()[0])
self.v_cache.set(self.selection.curselection()[0])
self.widgetstate()
self.selection.bind("<ButtonRelease-1>", index)
def v_cache_Callback(self, varName, index, mode):
self.v_zoneselection.set(self.v_cache.get())
def v_zoneselection_Callback(self, varName, index, mode):
self.v_cache.set(self.v_zoneselection.get())
def defaultvalue(self, objet):
if objet < self.selection.size():
self.selection.select_set(objet)
self.v_zoneselection.set(self.selection.get(objet))
self.v_cache.set(self.selection.get(objet))
# Init
def __init__(self, parent=None, width=None, border=1, background=None, foreground=None, fg=None, bg=None, font=None, relief=None, cursor=None, exportselection=None, selectbackground=None, selectforeground=None, height=None):
Toplevel.__init__(self, parent, bd=0)
self.withdraw()
self.parent = parent
self.overrideredirect(1)
# Inutile ?
self.transient()
# StringVar()
self.v_zoneselection = StringVar()
self.v_cache = StringVar()
# Interface
self.zoneselection = Entry(parent, border=border, fg=fg, bg=bg, font=font, relief=relief, cursor=cursor, exportselection=exportselection, selectbackground=selectbackground, selectforeground=selectforeground, height=height, width=width, background=background, foreground=foreground, highlightthickness=0, textvariable=self.v_zoneselection)
self.cache = Entry(self, border=border, fg=fg, bg=bg, font=font, relief=relief, cursor=cursor, exportselection=exportselection, selectbackground=selectbackground, selectforeground=selectforeground, width=width, height=height, background=background, foreground=foreground, highlightthickness=0, textvariable=self.v_cache)
self.cache.pack(fill=X)
self.listescroll = Scrollbar(self)
self.listescroll.pack(side=RIGHT, fill=Y)
self.selection = Listbox(self, yscrollcommand=self.listescroll.set, width=width, border=border, background=background, foreground=foreground, fg=fg, bg=bg, font=font, relief=relief, cursor=cursor, exportselection=exportselection, selectbackground=selectbackground, selectforeground=selectforeground, height=height)
self.selection.pack(side=LEFT, fill=X)
self.listescroll.config(command=self.selection.yview)
# Binds
self.selection.bind("<ButtonPress-1>", self.selectionner)
self.cache.bind("<ButtonPress-1>", self.widgetstate)
self.zoneselection.bind("<ButtonPress-1>", self.widgetstate)
# withdraw le toplevel si la fenetre est reduite
self.parent.bind("<Unmap>", self.reposition)
# withdraw le toplevel si on deplace la fenetre
self.parent.bind("<Configure>", self.reposition)
# Pour eviter que le toplevel reste afficher si une autre fenetre est selectionnee.
# A revoir
# self.parent.bind("<FocusOut>", self.reposition)
# Trace des StringVar()
self.v_zoneselection_StringVar_traceName = self.v_zoneselection.trace_variable("w", self.v_zoneselection_Callback)
self.v_cache_StringVar_traceName = self.v_cache.trace_variable("w", self.v_cache_Callback)
self.zoneselection.icursor(0)
# Init de self.valeur_widget_state
self.valeur_widget_state = True
self.widgetstate()
# Demo
class test(Tk):
def __init__(self):
Tk.__init__(self)
monexplication = """Sample: Tk.__init__(self)
self.explication = Text(self, bg='white')
self.explication.insert(END, monexplication)
self.explication.pack(side=TOP)
self.listemini = TkComboBox(self, width=10, bg='black', fg='white')
self.listemini.pack(side=TOP, pady=20)
self.listemini.config(bg='white', fg='black')
for index in range(0,100):
self.listemini.insert(index, index)
self.listemini.defaultvalue(50)
Button(self, text='QUIT', command=self.quit).pack(side=TOP, pady=20)"""
self.explication = Text(self, bg='white')
self.explication.insert(END, monexplication)
self.explication.pack(side=TOP)
self.listemini = TkComboBox(self, width=10, bg='black', fg='white')
self.listemini.pack(side=TOP, pady=20)
self.listemini.config(bg='white', fg='black')
for index in range(0,100):
self.listemini.insert(index, index)
self.listemini.defaultvalue(50)
Button(self, text='QUIT', command=self.quit).pack(side=TOP, pady=20)
if __name__ == '__main__':
app = test()
app.mainloop() |
Partager