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

时间:2015-07-13 21:54:11

标签: python user-interface tkinter

我正在尝试用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类(Frame),所以我将它显示在另一个框架上(并最终使用传递的信息做其他事情)。如何将信息从ScanItem类传递给Confirm类?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以通过使用属性样式函数(getter / setter)来实现(因为您的窗口具有干净的父子关系)。

我个人会避免像self.__controller.show_frame(Confirm)那样创建确认窗口。

您可以创建类self.__confirmFrame=Confirm()的类实例,然后在self.__confirmFrame上调用setter。

要获取值,您可以创建一个事件并绑定它或使用getter并调用它们。这里的最佳做法是(我的个人意见)两者的结合 - 例如在submit_btn点击事件中创建一个事件,然后将创建的事件(例如"<< submit >>")绑定在父框架中,以调用所需值的getter。 cancel_btn点击事件可以完成同样的工作(如有必要)。