Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- class ListChanger:
- def __init__(self):
- self.root = Tk()
- self.quit = False ##used for quitting the application
- self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##setting the [X] button to run code rather than tring to force quit the app
- self.list1 = ["1", "2", "3", "4"] ##some lists to use. this can be replaced by db queries insetad
- self.list2 = ["a", "b", "c", "d"]
- def list_changer(self, frame, option, lb): ##this will switch the lists around
- children = frame.winfo_children() ##gets all children of a widget
- for child in children: ## for all the children of the widget
- child.pack_forget() ##this only works if pack() is used rather than grid()
- child.destroy() ##destroys the widget from memory
- if option == "1": ##simple switch case
- lOPTIONS = self.list1
- elif option == "2":
- lOPTIONS = self.list2
- else:
- lOPTIONS = [None] #prevents any errors
- ##rebuiding the option menu with a new list
- lvariable = StringVar(frame)
- lvariable.set(lOPTIONS[0])
- ldrop = OptionMenu(*(frame, lvariable) + tuple(lOPTIONS))
- ldrop.pack(side=TOP)
- ##for listbox
- size = lb.size()
- for i in range(size):
- lb.delete(0)
- for i in range(len(lOPTIONS)): ##copyinh lOPTIONS form above but can be canged to any list
- lb.insert(i, lOPTIONS[i])
- 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
- option = strvar.get() ##get what option is currently selected
- self.list_changer(frame, option, lb) ##run the changer with the option and the holder Frame
- def main(self):
- baseOptions = Frame(self.root)
- baseOptions.pack(side=TOP)
- bOPTIONS = ["1", "2"]
- bvariable = StringVar(baseOptions)
- bvariable.set(bOPTIONS[0])
- bdrop = OptionMenu(*(baseOptions, bvariable) + tuple(bOPTIONS))
- bdrop.pack(side=TOP)
- holderFrame = Frame(self.root) ##this frame should contain the list you want to change. Makes deleting the optionMenu easier
- holderFrame.pack(side=TOP)
- lOPTIONS = self.list1 ##creating the changeable list using the default options.
- lvariable = StringVar(holderFrame)
- lvariable.set(lOPTIONS[0])
- ldrop = OptionMenu(*(holderFrame, lvariable) + tuple(lOPTIONS))
- ldrop.pack(side=TOP)
- holderFrame2 = Frame(self.root)
- holderFrame2.pack(side=TOP)
- lb = Listbox(holderFrame2)
- lb.pack()
- for i in range(len(self.list1)):
- lb.insert(i, self.list1[i])
- #the trace() function can be used on any var,(strvar, intvar etc.) and is an event generated when that var is changed.
- bvariable.trace("w", lambda a, b, c, e=Event(), v=bvariable, f=holderFrame, l=lb: self.tracer(a, b, c, e, v, f, l))
- while not self.quit: ## helps closing the application
- 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
- self.root.update()
- def quitting(self):
- self.quit = True
- if __name__ == "__main__":
- try:
- app = ListChanger()
- app.main()
- except Exception as err:
- print("There was an Error")
- print(err)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement