Python tkinter: Sorting the contents of an OptionMenu widget -


hello , in advance!

i've searched around google , read every result got , still can't figure
out, please @ least point me @ direction!
read pmw want see if there way tkinter first.

i'm writing simple enough program dnd dice rolls , have optionmenu
containing of dice needs play. have input field entering
die not included in default options. problem though new
option added successfully, options not sorted.

i solved @ point destroying optionmenu when new option added,
sorting list , rebuilding optionmenu scratch, using the
place manager method @ time , had rewrite program later because had
resolution problems. i'm using pack manager , destroying/rebuilding
not option unless want "re"pack widgets or make exclusive labels them!

here working sample of code:

from tkinter import *  class dropdownexample(frame):     def __init__(self, master = none):         frame.__init__(self, master)         self.pack(fill = 'both', expand = true)          # add option button         self.addoptbtn = button(self, text = "add option", command = self.add_option)          # option input field         self.newopt = intvar()         self.newopt.set("type number")          self.optin = entry(self)         self.optin['textvariable'] = self.newopt          # dropdown menu         self.myoptions = [0, 1, 2]          self.selopt = intvar()         self.selopt.set("options")          self.optmenu = optionmenu(self, self.selopt, *self.myoptions)          # positioning         self.addoptbtn.pack(side = 'left', padx = 5)         self.optin.pack(side = 'left', padx = 5)         self.optmenu.pack(side = 'left', padx = 5)       def add_option(self):         self.numtoadd = ""         self.counter = 0          try:             self.numtoadd = int(self.optin.get())                                           # integer validation              while self.counter < len(self.myoptions):                                       # comparison loop & error handling                 if self.numtoadd == self.myoptions[self.counter]:                     print("already exists!")                                         break;                  elif self.numtoadd < 0:                     print("no less 0!")                     break;                  elif self.counter < len(self.myoptions)-1:                     self.counter += 1                  else:                                                                       # dropdown menu option addition                     self.myoptions.append(self.numtoadd)                     self.myoptions.sort()                      self.optmenu['menu'].add_command(label = self.numtoadd)                      self.selopt.set(self.numtoadd)                      print("added succesfully!")                      self.counter += 2          except valueerror:             print("type numbers!")   def runme():     app = dropdownexample()     app.master.title("dropdown menu example")     app.master.resizable(0, 0)     app.mainloop()  runme() 

i using python 3.3 on windows 7

there set of insert_something() methods in menu. must keep list sorted each insert (bisect module).

from tkinter import * import bisect  ...                 else:                                                                       # dropdown menu option addition                     index = bisect.bisect(self.myoptions, self.numtoadd)                     self.myoptions.insert(index, self.numtoadd)                     self.optmenu['menu'].insert_command(index, label=self.numtoadd)                     self.selopt.set(self.numtoadd)                     print("added succesfully!", self.myoptions)                     self.counter += 2 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -