繁体   English   中英

将变量/值从一帧传递到另一帧— Tkinter / Python

[英]Pass variables/values from one frame to another — Tkinter/Python

我正在尝试使用Tkinter和python编写一个多窗口应用程序。 我已经设法创建了GUI,它从一页到另一页流动,但是我不知道如何将在一帧上收集的信息传递到另一帧。

这是我的主要应用程序类:

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框架:

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类(框架)中,以便将其显示在另一个框架上(并最终对传递的信息进行其他处理)。 如何将信息从ScanItem类传递给Confirm类?

提前致谢。

您可以通过使用属性样式函数(获取程序/设置程序)来实现此目的(因为您的窗口具有清晰的父子关系)。

我个人将避免创建诸如self.__controller.show_frame(Confirm)类的Confirm窗口。

您可以创建自己的类的实例,例如self.__confirmFrame=Confirm() ,然后在self.__confirmFrame上调用设置self.__confirmFrame

要获取值,您可以创建一个事件并对该事件进行绑定,也可以使用getter进行调用。 最佳实践是(根据我个人的观点)两者的结合-例如,在submit_btn click事件中创建一个事件,然后将创建的事件(例如"<< submit >>" )绑定到父框架中,以调用值的获取器你要。 可以对cancel_btn点击事件执行相同的操作(如有必要)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM