我希望这不是重复的,看来我是少数几个倾向于将Tkinter用作Tkinter而不是TK的人之一。 几乎我正在使用类制作Windows。第二个窗口创建一个用户,但是在另一个类中使用该值“ username”时遇到麻烦。 我想念什么?
(免责声明,窗户丑陋,我专注于使功能正常工作。)
我尝试将值username设置为clicku()
函数的全局部分。...我尝试将“ username”设置为Startwindow
下的一个函数(具有其自己的定义),直接将其拉出(如此处所示)...我已经移动了它,并尝试与地方合作...我迷路了。将条目作为自己的类可以保存此问题... ???
from tkinter import *
class Window(Frame):
def __init__(self, master=None, *args, **kwargs):
Frame.__init__(self,master,*args,**kwargs)
self.pack()
self.pack_propagate(0)
self.labelone = Label(self, text = "Welcome to our class! Click start to begin")
self.labelone.grid(row=1,column=1)
self.buttonone = Button(self, text="start", command=click)
self.buttonone.grid(row=2,column=1)
class Startwindow(Frame):
def __init__(self, master=None, *args, **kwargs):
Frame.__init__(self,master,*args,**kwargs)
self.grid(row=5,column=5)
self.grid_propagate(0)
self.text = Label(self, text = "What is your name?")
self.text.grid(row=0,column=0)
self.entry = Entry(self, width=15)
self.entry.grid(row=1,column=0)
self.buttontwo = Button(self, text="enter", command=clicku)
self.buttontwo.grid(row=1,column=1)
self.username = self.entry.get()
class introwindow(Frame):
def __init__(self, master=None,*args,**kwargs):
Frame.__init__(self,master,*args,**kwargs)
user = str(w2.username)
self.grid()
self.grid_propagate(0)
self.a = Label(self, text = "Aki: ")
self.a.grid(row = 0, column = 0)
self.sp1 = Label(self, text = "Hi" + w2.username)
self.sp1.grid(row = 0, column = 1)
self.sp2 = Label(self, text = "Nice to meet you!")
self.sp1.grid(row = 1, column = 1)
self.talk = Entry(self, width = 15)
def click():
global w2
w.destroy()
w2 = Startwindow(root,width=500,height=500)
def clicku():
w2.destroy()
w3 = introwindow(root,width=500,height=500)
root = Tk()
w = Window(root, bg="red",width=500,height=500)
root.mainloop()
我只是希望在introwindow
Label
中将用户名打印在sp1
中。
答案 0 :(得分:0)
您可以通过创建tkinter
StringVar
的全局实例来实现。这样做是对您的问题中张贴的代码(我也进行了更新以更严格地遵循PEP 8 - Style Guide for Python Code)。
您当前的设计不允许充分利用它们(这就是为什么我将其设置为全局变量)的原因。但是,为了将来参考,这里有一些additional information,以介绍如何使用它们。
from tkinter import *
class Window(Frame):
def __init__(self, master=None, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.pack()
self.pack_propagate(0)
self.labelone = Label(self, text="Welcome to our class! Click start to begin")
self.labelone.grid(row=1, column=1)
self.buttonone = Button(self, text="start", command=click)
self.buttonone.grid(row=2,column=1)
class StartWindow(Frame):
def __init__(self, master=None, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.grid(row=5,column=5)
self.grid_propagate(0)
self.text = Label(self, text="What is your name?")
self.text.grid(row=0,column=0)
self.entry = Entry(self, width=15, textvariable=username)
self.entry.grid(row=1,column=0)
self.buttontwo = Button(self, text="enter", command=clicku)
self.buttontwo.grid(row=1,column=1)
self.username = self.entry.get()
class IntroWindow(Frame):
def __init__(self, master=None, *args, **kwargs):
Frame.__init__(self,master, *args, **kwargs)
self.grid()
self.grid_propagate(0)
self.a = Label(self, text="Aki: ")
self.a.grid(row=0, column=0)
self.sp1 = Label(self, text="Hi " + username.get())
self.sp1.grid(row=0, column=1)
self.sp2 = Label(self, text="Nice to meet you!")
self.sp1.grid(row=1, column=1)
self.talk = Entry(self, width=15)
def click():
global w2
w.destroy()
w2 = StartWindow(root, width=500, height=500)
def clicku():
w2.destroy()
w3 = IntroWindow(root, width=500, height=500)
if __name__ == '__main__':
root = Tk()
username = StringVar()
w = Window(root, bg="red", width=500, height=500)
root.mainloop()
答案 1 :(得分:0)
由于条目窗口小部件是introwindow
的属性,因此您只需要在销毁窗口之前在 上调用条目窗口小部件get
的方法。然后,您可以将值传递给下一个窗口的构造函数
例如,它看起来像这样:
def clicku():
name = w2.entry.get()
print("clickU:", name)
w2.destroy()
w3 = IntroWindow(root, width=500, height=500, name=name)
当然,您需要修改IntroWindow
类以接受名称。