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
| 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, selectbackgroun, 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)
#
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()
# 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):
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.zoneselection.delete(0, END)
self.zoneselection.insert(0, self.selection.get(self.selection.curselection()))
self.cache.delete(0, END)
self.cache.insert(0, self.selection.get(self.selection.curselection()))
def selectionner(self, event):
def index(event):
try:
self.zoneselection.delete(0, END)
self.zoneselection.insert(0, self.selection.get(self.selection.curselection()))
self.cache.delete(0, END)
self.cache.insert(0, self.selection.get(self.selection.curselection()))
except:
pass
self.widgetstate()
self.selection.bind("<ButtonRelease-1>", index)
# 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, selectbackgroun=None, selectforeground=None, height=None):
Toplevel.__init__(self, parent, bd=0)
self.withdraw()
self.parent = parent
self.zoneselection = Entry(parent, border=border, fg=fg, bg=bg, font=font, relief=relief, cursor=cursor, exportselection=exportselection, selectbackgroun=selectbackgroun, selectforeground=selectforeground, height=height, width=width, background=background, foreground=foreground)
self.overrideredirect(1)
self.transient()
self.cache = Entry(self, border=border, fg=fg, bg=bg, font=font, relief=relief, cursor=cursor, exportselection=exportselection, selectbackgroun=selectbackgroun, selectforeground=selectforeground, height=height, width=width, background=background, foreground=foreground)
self.cache.pack()
self.listescroll = Scrollbar(self)
self.listescroll.pack(side=RIGHT, fill=Y)
self.selection = Listbox(self, yscrollcommand=self.listescroll.set, width=width-1, border=border, background=background, foreground=foreground, fg=fg, bg=bg, font=font, relief=relief, cursor=cursor, exportselection=exportselection, selectbackgroun=selectbackgroun, selectforeground=selectforeground, height=height)
self.selection.pack(side=LEFT)
self.listescroll.config(command=self.selection.yview)
self.selection.bind("<ButtonPress-1>", self.selectionner)
self.cache.bind("<ButtonPress-1>", self.widgetstate)
self.zoneselection.bind("<ButtonPress-1>", self.widgetstate)
self.valeur_widget_state = True
self.widgetstate()
self.parent.bind("<Unmap>", lambda event: self.withdraw())
self.parent.bind("<Configure>", lambda event: self.withdraw())
# Le widget ne disparait pas si une fenetre passe au premier plan.
# A revoir.
# self.bind("<Leave>", lambda event: self.withdraw())
# 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)
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)
Button(self, text='QUIT', command=self.quit).pack(side=TOP, pady=20)
if __name__ == '__main__':
app = test()
app.mainloop() |
Partager