我的问题是我希望我的代码接受用户的输入,
那么用户输入的值将显示在上一类的顶层页面上。
我已经搜索了很多,但是没有一种解决方案适合我。
那么,你们中的任何人都可以慷慨地告诉我如何更改代码,以便实际上可以在顶层页面上显示用户输入的值吗?

非常感谢

import Tkinter as tk


class Demo2:
    def __init__(self, master):
        self.master = master
        self.quitButton = tk.Button(self.master, text = 'Quit', width = 25, command = self.close_windows)
        self.goButton = tk.Button(self.master, text = "Go",  command = self.window_one)
        global EnteredNumber
        self.EnteredNumber = tk.IntVar()
        self.entry = tk.Entry(self.master,textvariable=self.EnteredNumber)
        VertexNumber = self.EnteredNumber.get()
        self.goButton.pack()
        self.entry.pack()
        self.quitButton.pack()

    def close_windows(self):
        self.master.destroy()

    def window_one(self):
        self.window_one = tk.Toplevel(self.master)
        self.app = Demo3(self.window_one)

class Demo3():
    def __init__(self, master):
        self.master = master
        self.label1 = tk.Label(self.master, text=EnteredNumber)
        self.label = tk.Label(self.master, text="Hi")
        self.label.pack()
        self.label1.pack()

def main():
    root = tk.Tk()
    app = Demo2(root)
    root.overrideredirect(True)
    root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
    root.mainloop()


if __name__ == '__main__':
    main()

最佳答案

Entry的值发送到__init__Demo3()

首先对window_one()进行更改:

def window_one(self):
    enterednumber = self.entry.get()    # Grab the value in the Entry
    self.window_one = tk.Toplevel(self.master)
    self.app = Demo3(self.window_one, enterednumber)   # Send it to Demo3 as an argument


然后Demo3()

class Demo3():
    def __init__(self, master, enterednumber):    # Reconfigure __init__ to accept the new arg
        self.master = master
        self.label1 = tk.Label(self.master, text=enterednumber)
        self.label = tk.Label(self.master, text="Hi")
        self.label.pack()
        self.label1.pack()

10-08 04:08