【发布时间】:2020-11-25 23:54:47
【问题描述】:
我正在尝试使用 Tkinter 创建一个应用程序,该应用程序要求用户点击第一个窗口的按钮,然后会出现一个新窗口,他们将在其中写下他们的名字。 但是当我尝试获取名称时,我总是以一个空字符串结束。 这是我的代码:
from tkinter import *
class first_class(object):
def __init__(self, window):
self.window = window
b1 = Button(window, text = "first_get", command = self.get_value_2)
b1.grid(row = 0, column = 1)
def get_value_2(self):
sc = Tk()
second_class(sc)
sc.mainloop()
class second_class(object):
def __init__(self, window):
def get_value_1():
print(self.name.get())
self.window = window
self.name = StringVar()
self.e1 = Entry(window, textvariable = self.name)
self.e1.grid(row = 0, column = 0)
b1 = Button(window, text = "second_get", command = get_value_1)
b1.grid(row = 0, column = 1)
window = Tk()
first_class(window)
window.mainloop()
我应该怎么做才能正确获得名称?
【问题讨论】: