函数内部的Python入口小部件



Hiii。使用这种方法,我无法获得输入变量中的文本。使用get方法,我总是得到0作为输入值。我也试过分部lambda函数!但是仍然无法获取入口值。有人请帮我解决这个问题!

from tkinter import *
a = Tk()
def reg():
global b
b = Tk()
global ab
ab = IntVar()
E = Entry(b,textvariable=ab).pack()
b2 = Button(b, text='submit',command=sub).pack()

def sub():
l = ab.get()
Lab = Label(b, text=l).pack()

b1 = Button(a, text='reg',command=lambda:reg()).pack()

我认为您实现第二个窗口b的方式导致了这个问题。tkinter应用程序中应该始终只有一个根窗口。所有其他窗口都应该是根窗口的顶层窗口小部件。

这个代码应该可以工作。

from tkinter import *
# Root window
a = Tk() # All other windows should be the toplevel widgets for this root window
def reg():
global b
b = Toplevel(a)
global ab
ab = IntVar()
E = Entry(b,textvariable=ab).pack()
b2 = Button(b, text='submit',command=lambda:sub()).pack()

def sub():
l = ab.get()
Lab = Label(b, text=l).pack()

b1 = Button(a, text='reg',command=lambda:reg()).pack()
a.mainloop()

观看此视频。使用tkinter对多窗口/框架gui有更好的实现和解释。

最新更新