在Tkinter 2.0的框架之间传递变量?

时间:2019-03-23 21:32:03

标签: python-3.x class tkinter

在Tkinter中的帧之间传递变量 遵循以下多帧类示例: 来自Switch between two frames in tkinter 布莱恩·奥克利(Bryan Oakley)/史蒂文·瓦斯切罗 在每个框架中,一切都可以正常工作,但是尝试将输入框架StartPage条目的文本传递到框架PageTwo-scroll中需要使用以下语法:

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

我不明白。

顺便说一句: 尝试按照原始解决方案进行操作: Passing variables between frames in Tkinter 通过添加

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

无济于事??????

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

0 个答案:

没有答案