1

Passing variables between frames in Tkinter Followed the multiframe class example in: from Switch between two frames in tkinter by Bryan Oakley / Steven M. Vascellaro Everything works fine within each frame, but trying to pass a Frame StartPage-entry text into the Frame PageTwo-scroll requires syntax in :

txtScroll(txt):  ??????scroll.insert(tk.INSERT,scrolltxt) 

I do not get.

BTW: Tried following the original solution to: Passing variables between frames in Tkinter by adding

def __init__(self, master,controller, uploadPage=None):
self.controller = controller
self.uploadPage = PageTwo

To no avail ??????

# from https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
# by Bryan Oakley / Steven M. Vascellaro

import tkinter as tk                # python 3
from tkinter import ttk, font  as tkfont, scrolledtext # python 3
from tkinter.ttk import *

#definitions ----------------------

def txtClicked(txt): 
    res = "Entry text " + txt.get()
    print(res)

def txtScroll(txt):     
    # Problem with output to scroll in different class ?
    scroll.insert(tk.INSERT,scrolltxt)

def scrollInsert(scroll,scrolltxt): 
    scroll.insert(tk.INSERT,scrolltxt)

#Frame class  ----------------------    
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.replace_frame(StartPage)

    def replace_frame(self, frame_class):
        """Destroys current frame and replaces i with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

class StartPage(tk.Frame):
    def __init__(self, master, uploadPage=None):
        tk.Frame.__init__(self, master)
        #self.controller = controller
        self.uploadPage = PageTwo
        tk.Label(self, text="This is the start page").pack(side="top", fill="x", pady=10)
        # Entry
        txt = Entry(self,width=10)
        txt.pack(side="top", fill="x", pady=10)
        btn = Button(self, text="Get Entry", command=lambda: txtClicked(txt)).pack()
        btn = Button(self, text="Scroll Output", command=lambda: txtScroll(txt)).pack()
        #-------------
        tk.Button(self, text="Open page one", command=lambda: master.replace_frame(PageOne)).pack()
        tk.Button(self, text="Open page two", command=lambda: master.replace_frame(PageTwo)).pack()

class PageOne(tk.Frame):
    def __init__(self, master, uploadPage=None):
        tk.Frame.__init__(self, master)
        #self.controller = controller
        self.uploadPage = PageTwo
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page", command=lambda: master.replace_frame(StartPage)).pack()

class PageTwo(tk.Frame):
    def __init__(self, master, uploadPage=None):
        tk.Frame.__init__(self, master)
        #self.controller = controller
        print ("tk.Frame: ", tk.Frame )
        print ("self: ", self )
        print ("master: ", master )

        # scrolledtext
        scroll = scrolledtext.ScrolledText(self,width=40,height=10) 
        scroll.pack(side="bottom", padx=10, pady=10)
        scrolltxt = "Print scroll stuff..."
        btn = Button(self, text="Print scroll", command=lambda: scrollInsert(scroll,scrolltxt)).pack()
        #-------------
        tk.Label(self, text="This is page two").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page", command=lambda: master.replace_frame(StartPage)).pack()

#Tkinder Main  ----------------------    
if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
user3005949
  • 117
  • 2
  • 12

0 Answers0