user interface - Pass variables/values from one frame to another -- Tkinter/Python -
i trying write multi-window application tkinter , python. have managed create gui , flows 1 page next don't know how pass information collected on 1 frame frame.
here main application class:
class sampleapp(tk.tk): def __init__(self, *args, **kwargs): tk.tk.__init__(self, *args, **kwargs) # container we'll stack bunch of frames # on top of each other, 1 want visible # raised above others 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 = {} f in (mainmenu, scanitem, account, confirm, removeitem): frame = f(container, self) self.frames[f] = frame # put of pages in same location; # 1 on top of stacking order # 1 visible. frame.grid(row=0, column=0, sticky="news") self.show_frame(mainmenu) def show_frame(self, c): # show frame given class frame = self.frames[c] frame.tkraise()
scanitem frame:
class scanitem(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) self.__barcode = tk.stringvar(none) barcode = tk.entry(self, textvariable=self.__barcode, font="helvetica 16 bold", justify="center") cancel_btn = tk.button(self, text="cancel", command=lambda: controller.show_frame(mainmenu)) tk.label(self, font="helvetica 16 bold", text="scan item").grid( row=0, column=0, columnspan=6, sticky="news") barcode.grid(row=2, column=1, columnspan=4, sticky="news") cancel_btn.grid(row=4, column=1, columnspan=4, sticky="news", pady=10) # focus cursor on barcode entry widget barcode.focus_set() # usb scanner output has <return> character @ end of barcode barcode.bind('<return>', (lambda event: self.lookup())) in range(5): self.grid_rowconfigure(i, weight=1) j in range(6): self.grid_columnconfigure(j, weight=1) def get_barcode(self): return self.__barcode def lookup(self): # tkmessagebox.showinfo("barcode scanned", self.__barcode.get()) self.__controller.show_frame(confirm)
confirm frame:
class confirm(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) barcode = tk.label(self, font="helvetica 8 bold", text="barcode scan item goes here") cancel_btn = tk.button(self, text="cancel", width=100, command=lambda: controller.show_frame(mainmenu)) submit_btn = tk.button(self, text="submit", width=100) tk.label(self, font="helvetica 16 bold", text="confirm barcode").grid( row=0, column=0, columnspan=6, sticky="news") barcode.grid(row=1, column=1, columnspan=4, sticky="news") cancel_btn.grid(row=2, column=0, columnspan=3, sticky="news") submit_btn.grid(row=2, column=3, columnspan=3, sticky="news") in range(3): self.grid_rowconfigure(i, weight=1) j in range(6): self.grid_columnconfigure(j, weight=1)
i barcode scanitem entry widget in confirm class (frame) display on frame (and other stuff information passed along). how pass information scanitem class confirm class?
thanks in advance.
you can achieve (as having clean parent-child relation of windows) using property style functions (getters/setters).
i avoid creating confirm window self.__controller.show_frame(confirm)
.
you create instance of class self.__confirmframe=confirm()
, call setters on self.__confirmframe
.
to values either create event , bind on or use getters , call them. best practice here (my personal opinion) combination of both - e.g. create event @ submit_btn
click event , bind created event (e.g. "<< submit >>"
) in parent frame call getters of values want. same done (if neccessary) on cancel_btn
click event.
Comments
Post a Comment