python - using function from module with button -Tkinter -
so doing learning modules. decent tkinter after using 5 months or so. can work if put functions inside main file. separating them separate modules can learn how work modules better. question more knowledge.
i going have 3 files total, main loop (example_gui.py) , pythonic functions (example_funcs.py) , gui functions (more_example_funcs.py)... can see issue using "get_text()" inside more_example_funcs.py obvious why doesnt work in case. variable not defined inside .py file. how make work? told better way code having functions inside file(modules).
with full scale app using tkinter , going have bunch of functions connected entries such going defined in example_gui.py easier if put functions inside more_example_funcs.py example below
example_funcs.py
from tkinter import * import tkinter tk def center(toplevel): toplevel.update_idletasks() w = toplevel.winfo_screenwidth() h = toplevel.winfo_screenheight() size = tuple(int(_) _ in toplevel.geometry().split('+')[0].split('x')) x = w/2 - size[0]/2 y = h/2 - size[1]/2 toplevel.geometry("%dx%d+%d+%d" % (size + (x, y))) def popupmsg(msg): popup = tk.toplevel() popup.title("information dialog") label = label(popup, text = msg) label.pack(side="top", pady=10) button = button(popup, text = "ok", command = popup.destroy) button.pack() popup.grab_set() center(popup) popup.mainloop()
more_example_funcs.py
from tkinter import * import tkinter tk def get_text(): print entry_one_var.get()
example_gui.py
from tkinter import * import tkinter tk import example_funcs ef import more_example_funcs mef class start(tk.tk): def __init__(self, *args, **kwargs): tk.tk.__init__(self, *args, **kwargs) container = tk.frame(self) container.pack(side="top", fill="both", expand = true) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} tk.tk.title(self, "app name") menubar = tk.menu(container) tk.tk.config(self, menu=menubar) filemenu = tk.menu(menubar, tearoff=0) menubar.add_cascade(label="file", menu=filemenu) filemenu.add_command(label="exit", command=quit) f in (page_one, page_two, page_three): frame = f(container, self) self.frames[f] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(page_one) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() class page_one(tk.frame): def __init__(self, parent, controller, *args, **kwargs): self.controller = controller frame.__init__(self, parent, *args, **kwargs) self.labels_one() self.buttons_one() self.entries_one() def labels_one(self): label1 = label(self, text="welcome page one") label1.grid() def buttons_one(self): button_one = button(self, text="go page two", command=lambda:self.controller.show_frame(page_two)) window_one_button = button(self, text="open popup window", command=lambda:ef.popupmsg("new window 1")) text_one_button = button(self, text="print entered text", command=mef.get_text) button_one.grid() window_one_button.grid() text_one_button.grid() def entries_one(self): entry_one_var=stringvar() entry_one = entry(self, textvariable= entry_one_var) entry_one.grid() class page_two(tk.frame): def __init__(self, parent, controller, *args, **kwargs): self.controller = controller frame.__init__(self, parent, *args, **kwargs) self.labels_two() self.buttons_two() def labels_two(self): label2 = label(self, text="welcome page two") label2.grid() def buttons_two(self): button_two = button(self, text="go page three", command=lambda:self.controller.show_frame(page_three)) window_two_button = button(self, text="open popup window", command=lambda:ef.popupmsg("new window 2")) button_two.grid() window_two_button.grid() class page_three(tk.frame): def __init__(self, parent, controller, *args, **kwargs): self.controller = controller frame.__init__(self, parent, *args, **kwargs) self.labels_three() self.buttons_three() def labels_three(self): label3 = label(self, text="welcome page three") label3.grid() def buttons_three(self): button_three = button(self, text="go page one", command=lambda:self.controller.show_frame(page_one)) window_three_button = button(self, text="open popup window", command=lambda:ef.popupmsg("new window 3")) button_three.grid() window_three_button.grid() app = start() ef.center(app) app.mainloop()
make get_text
function take arguments can call on variable later.
more_example_funcs.py
from tkinter import * import tkinter tk def get_text(var): print var.get()
also, make entry_one_var
in page_one
class variable using self
keyword (self.entry_one_var
) since you'll need in more 1 method, pass self.entry_one_var
argument when call get_text
.
this how page_one
class like:
class page_one(tk.frame): def __init__(self, parent, controller, *args, **kwargs): self.controller = controller frame.__init__(self, parent, *args, **kwargs) self.labels_one() self.buttons_one() self.entries_one() def labels_one(self): label1 = label(self, text="welcome page one") label1.grid() def buttons_one(self): button_one = button(self, text="go page two", command=lambda:self.controller.show_frame(page_two)) window_one_button = button(self, text="open popup window", command=lambda:ef.popupmsg("new window 1")) text_one_button = button(self, text="print entered text", command=lambda: mef.get_text(self.entry_one_var))## button_one.grid() window_one_button.grid() text_one_button.grid() def entries_one(self): self.entry_one_var=stringvar() #make entry_one_var class instance variable entry_one = entry(self, textvariable= self.entry_one_var) ## entry_one.grid()
i hope helps.
Comments
Post a Comment