python - Adding or deleting tkinter widgets from within other modules -
i'd know how can add or delete widgets within imported module. fail access them correctly. know, using oop make easier, tried grasp oop , while principles easy can't head around details, since lack proper teacher, need procedural solution.
this main script:
#!/usr/bin/python try: # python2 import tkinter tk except importerror: # python3 import tkinter tk import os import sys sys.path.append(os.path.dirname(os.path.realpath(__file__))) import target def myfunction(event): canvas.configure(scrollregion=canvas.bbox("all"),width=300,height=200) def test(): target.secondwindow() root = tk.tk() root.geometry("600x350+30+50") myframe = tk.frame(root,relief="groove",bd=1) myframe.place(x=20, y=30, width=560, height=200 ) canvas = tk.canvas(myframe) frame = tk.frame(canvas) myscrollbar=tk.scrollbar(myframe, orient="vertical", command=canvas.yview) canvas.configure(yscrollcommand=myscrollbar.set) myscrollbar.pack(side="right", fill="y") canvas.pack(side="left") canvas.create_window((0,0), window=frame, anchor='nw') allmissions = { "1":{"name":"go"}, "2":{"name":"see"}, "3":{"name":"win"}, "4":{"name":"party"}} # text file in allmissions.keys(): mn = allmissions[a]["name"] tk.label(frame, text=mn, justify="left").grid(row=int(a), column=0) # what's bind doing? frame.bind("<configure>", myfunction) test = tk.button(root, command=test, text="test") test.place(x = 20, y = 250, width=580, height=40) tk.mainloop()
and imported module: target.py
try: # python2 import tkinter tk except importerror: # python3 import tkinter tk def changemainwindow(): # here's i'm stuck print("what have add new") print("label in main window here?") print("or delete it?") def secondwindow(): amwin = tk.toplevel() amwin.geometry("300x200+720+50") button = tk.button(amwin, text="ok", command=changemainwindow) button.place(x = 20, y = 80, width=260, height=30) #amwin.mainloop() comment noticed (:
you passing memory address of whatever widget second program. there no reason import tkinter again can pass pointer existing instance. if going doing more simple experimenting tkinter, worth time learn classes first @ 1 of online sites 1 http://www.greenteapress.com/thinkpython/html/thinkpython016.html more here https://wiki.python.org/moin/beginnersguide/nonprogrammers
aren't going many answers way program structured because programmers use class structure afaik, not know how pound code non-class environment, not have answers. if first program below used classes second program's class inherited, , functions become part of first program's class , accessed in same way existing classes, no passing of pointers, or other hack, necessary.
## deleted code simplicity def myfunction(event): canvas.configure(scrollregion=canvas.bbox("all"),width=300,height=200) def test(): tg.secondwindow() root = tk.tk() root.geometry("600x350+30+50") myframe = tk.frame(root,relief="groove",bd=1) myframe.place(x=20, y=30, width=560, height=200 ) canvas = tk.canvas(myframe) frame = tk.frame(canvas) myscrollbar=tk.scrollbar(myframe, orient="vertical", command=canvas.yview) canvas.configure(yscrollcommand=myscrollbar.set) myscrollbar.pack(side="right", fill="y") canvas.pack(side="left") canvas.create_window((0,0), window=frame, anchor='nw') # what's bind doing? frame.bind("<configure>", myfunction) test = tk.button(root, command=test, text="test", bg="lightblue") test.place(x = 20, y = 250, width=580, height=40) tk.button(root, text="quit all", command=root.quit, bg="orange").place(x=20, y=300) """ instance of class in imported program pointer root window , tk instance passed """ tg=target.target(tk, root) tk.mainloop()
and target.py. notice there no imports.
class target(): def __init__(self, tk, root): self.tk=tk self.root=root def changemainwindow(self): # here's i'm stuck self.tk.label(self.amwin, bg="yellow", text =""""what have add new label in main window here? or delete it?""").place(x=50,y=20) def secondwindow(self): self.amwin = self.tk.toplevel(self.root) self.amwin.geometry("300x200+720+50") button = self.tk.button(self.amwin, text="add label", command=self.changemainwindow) button.place(x = 20, y = 90, width=260, height=30).
Comments
Post a Comment