gpt4 book ai didi

python - StringVar.set ("...")在多个 tkinter 窗口中不起作用

转载 作者:行者123 更新时间:2023-11-30 21:49:40 25 4
gpt4 key购买 nike

我的程序有一个打开新窗口的库

这是库(称为 make_entry):

from tkinter import *

def Create():
Window = Tk() # window
Window.geometry("900x500+50+50") # heightxwidth+x+y

mainPanel = Canvas(Window, width = 900, height = 500) # main screen
mainPanel.pack()

anyvar = StringVar() # the text in the entry
entry = Entry(mainPanel, width = 40, font = ("Purisa", 12, "bold"), justify = "center", textvariable = anyvar) # the entry
mainPanel.create_window(200, 100, window = entry)
anyvar.set("This doesnt work!!!!!")

Window.mainloop()

#Create()

如果我单独运行这个库,那么一切都会正常工作,但是当我从另一个程序导入它时,唯一不起作用的是 anyvar.set("This does not work!!!!!") .

这是我导入它的位置:(大部分代码已被删除)

from tkinter import *
Window = Tk()
import make_entry
make_entry.Create()
Window.mainloop()

有没有办法在不删除任何窗口的情况下解决此问题?

最佳答案

您有两个 Tk() 实例,这使 Tkinter 感到困惑。我猜测 StringVar() 将转到另一个(第一个)实例。相反,将唯一的实例传递给函数,并为新窗口使用 Toplevel。

from tkinter import *

def Create(root):
window=Toplevel(root)
window.geometry("900x500+50+50") # heightxwidth+x+y

mainpanel = Canvas(window, width = 900, height = 500) # main screen
mainpanel.pack()

anyvar = StringVar() # the text in the entry
entry = Entry(mainpanel, width = 40, font = ("Purisa", 12, "bold"), justify = "center", textvariable = anyvar) # the entry
mainpanel.create_window(200, 100, window = entry)
anyvar.set("This doesnt work!!!!!")

from tkinter import *
Window = Tk()
import make_entry
make_entry.Create(Window)
Window.mainloop()

关于python - StringVar.set ("...")在多个 tkinter 窗口中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34423172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com