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
| 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) => int
# curselection() => int
# delete(objet=["ALL" ou int]
# start=int
# end=["END" ou int]
# focus_set()
# focus()
# 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)
#
class TkComboBox(Toplevel):
listeobjets = []
def activate(self, index):
self.selection.activate(index)
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_focus(self):
self.zoneselection.get_focus()
def focus(self):
self.zoneselection.get_focus()
def get(self):
return self.zoneselection.get()
def pack(self, padx=None, pady=None, fill=None, expand=None, side=None):
self.conteneur.pack(padx=padx, pady=pady, fill=fill, expand=expand, side=side)
self.affiche()
def grid(self, row=None, column=None, columnspan=None, rowspan=None, sticky=None, padx=None, pady=None):
self.conteneur.grid(row=row, column=column, columnspan=columnspan, rowspan=rowspan, sticky=sticky, padx=padx, pady=pady)
self.affiche()
def place(self, relheight=None, relwidth=None, relx=None, rely=None, width=None, height=None, anchor=None, x=None, y=None):
self.conteneur.place(relheight=relheight, relwidth=relwidth, relx=relx, rely=rely, width=width, height=height, anchor=anchor, x=x, y=y)
self.affiche()
def affiche(self):
self.update_idletasks()
self.geometry('+%d+%d'%(self.conteneur.winfo_rootx(),self.conteneur.winfo_rooty()))
self.selection.bind("<ButtonPress-1>", self.selectionner)
self.zoneselection.bind("<ButtonPress-1>", self.widgetstate)
self.valeur_widget_state = True
self.widgetstate()
self.deiconify()
del(self.conteneur)
def size(self):
return self.selection.size()
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()))
def selectionner(self, event):
def index(event):
try:
self.zoneselection.delete(0, END)
self.zoneselection.insert(0, self.selection.get(self.selection.curselection()))
except:
pass
self.widgetstate()
self.selection.bind("<ButtonRelease-1>", index)
def widgetstate(self, event=None):
# True : Visible
# False : Cache la listbox
if self.valeur_widget_state == True:
self.valeur_widget_state = False
self.listescroll.pack_forget()
self.selection.pack_forget()
elif self.valeur_widget_state == False:
self.valeur_widget_state = True
self.listescroll.pack(side=RIGHT, fill=Y)
self.selection.pack(fill=X)
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.parent = parent
self.conteneur = Label(parent, bg=bg, width=width, height=height)
self.withdraw()
self.overrideredirect(1)
self.transient()
self.zoneselection = Entry(self, width=None, 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.zoneselection.pack(fill=X)
self.listescroll = Scrollbar(self)
self.listescroll.pack(side=RIGHT, fill=Y)
self.selection = Listbox(self, yscrollcommand=self.listescroll.set, width=None, 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(fill=X)
self.listescroll.config(command=self.selection.yview)
class test(Tk):
def __init__(self):
Tk.__init__(self)
monexplication = """ Tk.__init__(self)
self.geometry(str(self.winfo_screenwidth())+'x'
+str(self.winfo_screenheight())+'+0+0')
self.explication = Text(self, bg='white')
self.explication.insert(END, monexplication)
self.explication.pack(side=TOP)
self.listeMini = MonComboBox(self, width=200, bg='black', fg='white')
self.listeMini.pack(side=TOP, pady=20)
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)
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