Tkinter .set 和 .get 在窗口内的窗口中不起作用 [英] Tkinter .set and .get not working in a window inside a window

查看:62
本文介绍了Tkinter .set 和 .get 在窗口内的窗口中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from tkinter import *定义乐趣():trywindow=Tk()s=StringVar()entry=Entry(trywindow, textvariable=s)s.set("打印")entry.pack()trywindow.mainloop()根=Tk()乐趣()root.mainloop()

根据我执行此代码后,第二个窗口应显示输入块,其中包含已打印"文本,但为空白.

解决方案

如评论中所述,不鼓励使用多个 Tk() 实例.它会导致人们意想不到的行为,这个问题就是一个很好的例子.

此答案中所述,Tk 的所有实例都是完全隔离的.属于"其中之一的对象无法在其他对象中看到或使用.
在您的代码中发生的情况是您有两个 Tk 实例:roottrywindow.然后创建一个 StringVar,不带任何参数.这是执行此操作的常用方法,但您实际上可以在构建过程中提供一个主小部件.这样,您可以控制您的 StringVar 属于"哪个 Tk 实例.请参阅 effbot 中的引用:

<块引用>

构造函数参数仅在您使用多个 Tk 实例运行 Tkinter 时才相关(您不应该这样做,除非您真的知道自己在做什么).

如果您不指定主控,则会隐式选择主控.我相信它总是 Tk 的第一个创建实例.在你的情况下, StringVar 是用 root 作为它的主人创建的.由于这些 Tk 实例是完全分离的,trywindow 和其中的所有小部件都无法看到"StringVar 或其中的任何值.>

所以你可以通过简单地将 trywindow 传递给 SringVar 构造来修复你的代码:

s=StringVar(trywindow)

但是,将 trywindowTk 实例更改为 Toplevel 小部件可能更容易.这也会创建一个新窗口,但它属于同一个 Tk 实例,因此使用单独的 Tk 实例不会遇到这些困难:

from tkinter import *定义乐趣():trywindow = Toplevel()s = StringVar()条目 = 条目(尝试窗口,文本变量 = s)s.set("打印")entry.pack()trywindow.mainloop()根 = Tk()乐趣()root.mainloop()

from tkinter import *

def fun():
    trywindow=Tk()
    s=StringVar()
    entry=Entry(trywindow, textvariable=s)
    s.set("print")
    entry.pack()
    trywindow.mainloop()

root=Tk()
fun()

root.mainloop()

According to me after executing this code 2nd window should show enter block with text written in it "PRINTED" but it is blank.

解决方案

As mentioned in the comments, using multiple instances of Tk() is discouraged. It leads to behavior that people don't expect, of which this question is a great example.

As explained in this answer, all instances of Tk are completely isolated. Objects "belonging" to one of them can not be seen or used in the others.
What happens in your code is that you have two Tk instances: root and trywindow. Then you create a StringVar, without any arguments. This is the usual way to do this, but you actually can supply a master widget during construction. This way, you can control to which Tk instance your StringVar "belongs". See this quote from effbot:

The constructor argument is only relevant if you’re running Tkinter with multiple Tk instances (which you shouldn’t do, unless you really know what you’re doing).

If you don't specify the master, a master is chosen implicitly. I do believe it is always the first created instance of Tk. In your case, the StringVar is created with root as its master. Because these Tk instances are completely separated, trywindow and all widgets in it can not "see" the StringVar or any value in it.

So you could fix your code by simply passing trywindow to the SringVar constructuion:

s=StringVar(trywindow)

However, it's probably easier to change trywindow from a Tk instance to a Toplevel widget. This also creates a new window, but it belongs to the same Tk instance so you don't have these difficulties with separate Tk instances:

from tkinter import *

def fun():
    trywindow = Toplevel()
    s = StringVar()
    entry = Entry(trywindow, textvariable=s)
    s.set("print")
    entry.pack()
    trywindow.mainloop()

root = Tk()
fun()

root.mainloop()

这篇关于Tkinter .set 和 .get 在窗口内的窗口中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆