关于 tkinter StringVar() [英] About tkinter StringVar()

查看:331
本文介绍了关于 tkinter StringVar()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 tkinter StrinVar() 时遇到问题,我对 tkinter 了解不多,需要一些帮助,谢谢.

i am having issue with the tkinter StrinVar() and i dont have much knowledge in tkinter and would like some help, thanks.

我所做的是我创建了两个窗口,其中每个窗口都有一个 Entry 小部件和一个 Button 小部件,如果您单击一个窗口的按钮,则该窗口将退出并弹出另一个窗口,如果您单击该弹出窗口中的按钮,则它退出并弹出一个原始窗口.这是代码:-

What i have done is i created two window in which each has a Entry widget and Button widget if you click button of one window then that window gets withdraw and another window pop ups and if you click button in that pop up window then it withdraws and a original window gets pop up.Here is the code:-

from tkinter import *
def x():
    a1.set("")
    a.withdraw()
    b.deiconify()
def y():
    b1.set("")
    b.withdraw()
    a.deiconify()
a=Tk()
a.withdraw()
a1=StringVar()
b1=StringVar()
Entry(a,textvariable=a1).pack()
Button(a,text="button1",command=x).pack()
a.withdraw()
b=Tk()
Entry(b,textvariable=b1).pack()
Button(b,text="button2",command=y).pack()
mainloop()

如果您在条目小部件中写入内容并按下按钮 2,则会弹出第二个窗口,如果您在条目小部件中写入内容并按下按钮 1,则输入字段不会在前一个窗口中更新.

if you write something in entry widget and press button2 then second window pop ups then again if you write something in entry widget and press button1 the entry field is not getting updated in previous first window.

我发现有人建议不应该有两个 Tk() 实例,一个 tkinter 必须只有一个 Tk() 实例才能使用多个窗口 Toplevel().因此,我将 b=Tk() 更改为 b=Toplevel() 工作正常并且 StringVar() 正在更新值

i found that someone suggested that there should not be two instanses of Tk() a tkinter must have only one instanse of Tk() for multiple window use Toplevel(). So, i changed b=Tk() to b=Toplevel() which works fine and StringVar() was updating value

现在,我的问题是 1) 不使用 Tk()2) 的两个实例更新 StingVar() 背后的原因/逻辑是什么?使 StringVar() 更新具有两个 Tk()

Now, my question is that 1)What is the reason/logic behind not updating StingVar() with two instanses of Tk()2) is there any way to make StringVar() to update value with having two instanses of Tk()

推荐答案

" Tkinter 只是一个 Python 包装器,它封装了一个嵌入的 Tcl 解释器,它导入了 Tk 库.当你创建一个根窗口时,你就创建了一个 Tcl 解释器的实例.

" Tkinter is just a python wrapper around an embedded Tcl interpreter that imports the Tk library. When you create a root window, you create an instance of a Tcl interpreter.

每个 Tcl 解释器都是一个独立的沙箱.一个沙箱中的对象不能与另一个沙箱中的对象交互.最常见的表现是在一个解释器中创建的 StringVar 在另一个解释器中不可见. 小部件也是如此——您不能在一个解释器中创建小部件,而该小部件在另一个解释器中作为父小部件.

Each Tcl interpreter is an isolated sandbox. An object in one sandbox cannot interact with objects in another. The most common manifestation of that is that a StringVar created in one interpreter is not visible in another. The same goes for widgets -- you can't create widgets in one interpreter that has as a parent widget in another interpreter.

从技术角度来看,没有理由不能同时拥有两个 Tk 实例.反对它的建议是因为很少有实际需要有两个或更多不同的 Tcl 解释器,并且它会导致初学者难以掌握的问题.

From a technical standpoint, there's no reason why you can't have two instances of Tk at the same time. The recommendation against it is because there's rarely an actual need to have two or more distinct Tcl interpreters, and it causes problems that are hard for beginners to grasp.

在 99.9% 的情况下,最好的解决方案是创建一个在程序生命周期中使用的 Tk 实例.很简单,这就是 tkinter 和底层 Tcl/Tk 解释器的设计用途." --- 来自 Bryan Oakley为什么不鼓励使用多个 Tk 实例?

The best solution 99.9% of the time is to create exactly one instance of Tk that you use for the life of your program. Quite simply, that is how tkinter and the underlying Tcl/Tk interpreter was designed to be used. " --- From Bryan Oakley at Why are multiple instances of Tk discouraged?

#1 所以当你创建 a=Tk() 然后 b=Tk() StringVar a1, b1b=Tk() 中可用,但在 a=Tk() 中不可用.这就是 a=Tk() 中的值没有更新的原因.

#1 So When you create a=Tk() and then b=Tk() StringVar a1, b1 is available in b=Tk() but not in a=Tk(). that's why value is not updating in a=Tk().

#2 如上所述在一个解释器中创建的 StringVar 在另一个解释器中不可见".那么有没有办法让 StringVar() 用两个 Tk() 实例来更新值.

#2 As stated above "StringVar created in one interpreter is not visible in another". So is there no way to make StringVar() to update value with having two instanses of Tk().

这篇关于关于 tkinter StringVar()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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