Advertisement
AceScottie

optionmenu.py

Sep 5th, 2019
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. class ListChanger:
  4.     def __init__(self):
  5.         self.root = Tk()
  6.         self.quit = False ##used for quitting the application
  7.         self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##setting the [X] button to run code rather than tring to force quit the app
  8.         self.list1 = ["1", "2", "3", "4"] ##some lists to use. this can be replaced by db queries insetad
  9.         self.list2 = ["a", "b", "c", "d"]
  10.  
  11.     def list_changer(self, frame, option, lb): ##this will switch the lists around
  12.         children = frame.winfo_children() ##gets all children of a widget
  13.         for child in children: ## for all the children of the widget
  14.             child.pack_forget() ##this only works if pack() is used rather than grid()
  15.             child.destroy() ##destroys the widget from memory
  16.        
  17.         if option == "1": ##simple switch case
  18.             lOPTIONS = self.list1
  19.         elif option == "2":
  20.             lOPTIONS = self.list2
  21.         else:
  22.             lOPTIONS = [None] #prevents any errors
  23.        
  24.         ##rebuiding the option menu with a new list
  25.         lvariable = StringVar(frame)
  26.         lvariable.set(lOPTIONS[0])
  27.         ldrop = OptionMenu(*(frame, lvariable) + tuple(lOPTIONS))
  28.         ldrop.pack(side=TOP)
  29.        
  30.         ##for listbox
  31.         size = lb.size()
  32.         for i in range(size):
  33.             lb.delete(0)
  34.         for i in range(len(lOPTIONS)): ##copyinh lOPTIONS form above but can be canged to any list
  35.             lb.insert(i, lOPTIONS[i])
  36.        
  37.     def tracer(self, a, b, c, event, strvar, frame, lb): #a, b and c are auto generated from the trace() method and can be ignored but are required
  38.         option = strvar.get() ##get what option is currently selected
  39.         self.list_changer(frame, option, lb) ##run the changer with the option and the holder Frame
  40.  
  41.     def main(self):
  42.         baseOptions = Frame(self.root)
  43.         baseOptions.pack(side=TOP)
  44.         bOPTIONS = ["1", "2"]
  45.         bvariable = StringVar(baseOptions)
  46.         bvariable.set(bOPTIONS[0])
  47.         bdrop = OptionMenu(*(baseOptions, bvariable) + tuple(bOPTIONS))
  48.         bdrop.pack(side=TOP)
  49.        
  50.         holderFrame = Frame(self.root) ##this frame should contain the list you want to change. Makes deleting the optionMenu easier
  51.         holderFrame.pack(side=TOP)
  52.        
  53.         lOPTIONS = self.list1 ##creating the changeable list using the default options.
  54.         lvariable = StringVar(holderFrame)
  55.         lvariable.set(lOPTIONS[0])
  56.         ldrop = OptionMenu(*(holderFrame, lvariable) + tuple(lOPTIONS))
  57.         ldrop.pack(side=TOP)
  58.        
  59.        
  60.         holderFrame2 = Frame(self.root)
  61.         holderFrame2.pack(side=TOP)
  62.         lb = Listbox(holderFrame2)
  63.         lb.pack()
  64.         for i in range(len(self.list1)):
  65.             lb.insert(i, self.list1[i])
  66.        
  67.         #the trace() function can be used on any var,(strvar, intvar etc.) and is an event generated when that var is changed.
  68.         bvariable.trace("w", lambda a, b, c, e=Event(), v=bvariable, f=holderFrame, l=lb: self.tracer(a, b, c, e, v, f, l))
  69.        
  70.         while not self.quit: ## helps closing the application
  71.             self.root.update_idletasks() ##do not use root.mainloop() as it breaks other stuff such as sql, ftp and some web stuff by force closing the connection unsafly
  72.             self.root.update()
  73.            
  74.     def quitting(self):
  75.         self.quit = True
  76.        
  77. if __name__ == "__main__":
  78.     try:
  79.         app = ListChanger()
  80.         app.main()
  81.     except Exception as err:
  82.         print("There was an Error")
  83.         print(err)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement