[英]String Variable not setting initial value
class Lay():
def __init__(self):
root=Tk()
root.configure(background="black")
var=StringVar()
var.set("OVERVIEW")
Label(root,textvariable=var).grid(row=1,column=1,sticky=W+E+N+S)
Entry(root, textvariable = var).place(rely=1.0,relx=1.0,x=0,y=0,anchor=SE)
root.mainloop()
Hello, when i run this the initial value of the string variable does not appear, but when i type into the entry box, the text i type appears in the label.你好,当我运行这个时,字符串变量的初始值没有出现,但是当我在输入框中输入时,我输入的文本出现在标签中。 I'm not quite sure why this occurs, but i get an empty label to begin with, with the entry box.
我不太确定为什么会发生这种情况,但我首先得到一个空标签,带有输入框。 Thank you for any help.
感谢您的任何帮助。
Although, I couldn't reproduce the problem, I refactored your code to initialize tkinter widgets through a class(inspired by the snippet in the docs ) and also increased the window size so that the widgets are clearly viewed.虽然,我无法重现该问题,但我重构了您的代码以通过一个类初始化 tkinter 小部件(受docs 中的代码段启发)并且还增加了窗口大小,以便可以清楚地查看小部件。 If there is anything else in your code that is calling multiple windows as @jasonharper suggested, you should share that.
如果您的代码中有任何其他内容像@jasonharper 建议的那样调用多个窗口,您应该分享它。
import tkinter as tk
class Lay(tk.Tk):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.var=tk.StringVar()
self.var.set("OVERVIEW")
self.Widgets()
def Widgets(self):
self.displaylbl = tk.Label(self,textvariable=self.var)
self.displaylbl.grid(row=2,column=1,sticky=tk.W+tk.E+tk.N+tk.S)
self.entry = tk.Entry(self, textvariable = self.var)
self.entry.place(rely=1.0,relx=1.0,x=0,y=0,anchor=tk.SE)
app = Lay()
app.geometry("200x200")
app.mainloop()
Output:输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.