问题描述
我正在尝试使用 Tkinter 和 python 编写一个多窗口应用程序.我已经设法创建了 GUI,它从一个页面流向了下一个页面,但我不知道如何将在一个框架上收集的信息传递到另一个框架.
I am trying to write a multi-window application with Tkinter and python. I have managed to create the GUI and it flows from one page to the next but I don't know how to pass information collected on one frame to another frame.
这是我的主要应用程序类:
Here is my main application class:
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the 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 = {}
for F in (MainMenu, ScanItem, Account, Confirm, RemoveItem):
frame = F(container, self)
self.frames[F] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="news")
self.show_frame(MainMenu)
def show_frame(self, c):
# Show a frame for the given class
frame = self.frames[c]
frame.tkraise()
ScanItem 框架:
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 a <Return> character at the end of the barcode
barcode.bind('<Return>', (lambda event: self.lookup()))
for i in range(5):
self.grid_rowconfigure(i, weight=1)
for 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)
确认框架:
class Confirm(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
barcode = tk.Label(self, font="Helvetica 8 bold",
text="Barcode from 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")
for i in range(3):
self.grid_rowconfigure(i, weight=1)
for j in range(6):
self.grid_columnconfigure(j, weight=1)
我想将来自 ScanItem Entry 小部件的条形码放入 Confirm 类 (Frame),以便我将其显示在另一个框架上(并最终使用传递的信息执行其他操作).如何将信息从 ScanItem 类传递到 Confirm 类?
I would like to get the barcode from the ScanItem Entry widget in to the Confirm class (Frame) so I display it on another frame (and eventually do other stuff with the information passed along). How do I pass the information from the ScanItem class to the Confirm class?
提前致谢.
推荐答案
您可以通过使用属性样式函数(getter/setter)来实现(因为您的窗口具有干净的父子关系).
You can achieve that (as you are having a clean parent-child relation of your windows) by using property style functions (getters/setters).
我个人会避免像 self.__controller.show_frame(Confirm)
那样创建确认窗口.
I personally would avoid creating the Confirm window like self.__controller.show_frame(Confirm)
.
您可以创建一个类的实例,例如 self.__confirmFrame=Confirm()
,然后在 self.__confirmFrame
上调用 setter.
You could create an instance of your class like self.__confirmFrame=Confirm()
and then call the setters on self.__confirmFrame
.
要取回值,您可以创建一个事件并绑定在该事件上,或者使用 getter 并调用它们.这里的最佳实践是(我个人的意见)两者的结合 - 例如在 submit_btn
单击事件处创建一个事件,然后在您的父框架中绑定创建的事件(例如 "<< submit >>"
)以调用值的 getter你要.同样可以在 cancel_btn
点击事件上完成(如果需要).
To get values back you could either create a event and bind on that or use getters and call them. Best practice here would be (my personal opinion) a combination of both - e.g. create an event at submit_btn
click event and then bind the created event (e.g. "<< submit >>"
) in your parent frame to call the getters of the values you want. Same could be done (if neccessary) on cancel_btn
click event.
这篇关于将变量/值从一帧传递到另一帧——Tkinter/Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!