1

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 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)

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 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)

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?

Thanks in advance.

C_Rod_27
  • 61
  • 3
  • 5

1 Answers1

0

You can achieve that (as you are having a clean parent-child relation of your windows) by using property style functions (getters/setters).

I personally would avoid creating the Confirm window like self.__controller.show_frame(Confirm).

You could create an instance of your class like self.__confirmFrame=Confirm() and then call the setters on self.__confirmFrame.

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.

R4PH43L
  • 2,122
  • 3
  • 18
  • 30