Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在从一个窗口传递到另一个窗口以获取输入值时执行函数_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x 在从一个窗口传递到另一个窗口以获取输入值时执行函数

Python 3.x 在从一个窗口传递到另一个窗口以获取输入值时执行函数,python-3.x,tkinter,Python 3.x,Tkinter,我想在从第1页传递到第2页时获取输入字段中的文本,并将其作为参数传递到第2页: 这是我的密码 import tkinter as tk class MyApp(tk.Tk): def __init__(self): tk.Tk.__init__(self) container = tk.Frame(self) container.pack(side = "top", fill = "both", expand = True)

我想在从第1页传递到第2页时获取输入字段中的文本,并将其作为参数传递到第2页:

这是我的密码

import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)
        self.frames ={}
        for f in  (Page1, Page2):
            frame = f(container,self)
            self.frames[f] = frame
            frame.grid(row = 1000, column = 500, sticky = "nsew")
        self.show_frame(Page1)

    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()


class Page1(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        Frame0 = tk.Frame(self)
        Frame0.pack()
        Frame1 = tk.Frame(self)
        Frame1.pack()
        tk.Label(Frame0,text = "Page 1").pack()
        v = tk.StringVar()
        def say_hello():
            print(v.get())
        e = tk.Entry(Frame0, textvariable = v).pack()
        tk.Button(Frame1,text = "Page 2", command = (lambda: controller.show_frame(Page2))and say_hello).pack()

class Page2(tk.Frame) :
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        Frame0 = tk.Frame(self)
        Frame0.pack()
        Frame1 = tk.Frame(self)
        Frame1.pack()
        tk.Label(Frame0, text="Page 2").pack()
        tk.Button(Frame1,text = "Page 1", command = (lambda: controller.show_frame(Page1))).pack()

app = MyApp()
app.mainloop()
请问这个怎么做


(对不起,我的英语不好)

如果您希望
Page1
条目框内容在
Page2
中也可用,那么最好的选择是在
Page1
Page2
的公共控制器实例中定义并存储相应的StringVar:

class MyApp(tk.Tk):
    def __init__(self):
        ...
        # Store the StringVar in MyApp's instance
        self.v = tk.StringVar()
        ...
然后您可以在
Page1
中以
controller.v
的身份访问它:

class Page1(tk.Frame):
    def __init__(self,parent,controller):
        ...
        tk.Entry(Frame0, textvariable = controller.v).pack()
        tk.Button(Frame1,text = "Page 2", 
                  command=lambda: controller.show_frame(Page2)).pack()
以及
Page2

class Page2(tk.Frame) :
    def __init__(self,parent,controller):
        ...
        tk.Button(Frame0, text="Print Entry Value", 
                  command=lambda: print(controller.v.get())).pack()

“将其作为参数传递”是什么意思?在本例中,
Page2
是在用户有机会输入任何数据之前创建的。您想用参数调用什么函数?以下答案是否回答了您的问题?我想在第2页中参照我在第1页中创建的复选框创建一个按钮,这可能吗?使用这种代码结构,我可以在第2页中参照我在第1页中创建的复选框创建一个按钮(例如)吗?是的,实际上,在我的回答中,
Page2
类中的按钮就是这样做的:)