[英]The Entry fields are not being updated using user-defined modules
I have a master program in python3 that calls sub-programs via modules. 我在python3中有一个主程序,该主程序通过模块调用子程序。 This works fine, except that my modules don't update the Entry fields when changes are made elsewhere within the module. 效果很好,除了在模块中其他位置进行更改时,我的模块不会更新Entry字段。 The Module shows that values are put in the correct place, but the window isn't updated. 模块显示值已放置在正确的位置,但窗口未更新。 Here is an example of the master program:- 这是主程序的示例:
# test - master
from tkinter import *
root=Tk()
root.title("Test for the update problem")
import changeEntryfield
changeEntryfield.fun() # call to function inside module
root.mainloop()
# end of master program
And here is the module that is called (stored as "changeEntryfield.py"):- 这就是被调用的模块(存储为“ changeEntryfield.py”):
def fun():
import tkinter
win=tkinter.Tk() # start a window
g3=tkinter.StringVar()
g3.set("text")
a="something"
field=tkinter.Entry(win,width=10,textvariable=g3)
field.grid(column=1,row=0,sticky='W',padx=5,pady=5)
grpslab3=tkinter.Label(win,text="The Field is: ")
grpslab3.grid(column=0,row=0,sticky='W',padx=5,pady=5)
# now do changes to field value
g3.set(a)
print ("1. In Entry, field = ",field['textvariable'])
field['textvariable']=g3.get()
print ("2. In Entry, field = ",field['textvariable'])
field.config(textvariable=g3.get())
print ("3. In Entry, field = ",field['textvariable'])
field['textvariable']=a
print ("4. In Entry, field = ",field['textvariable'])
win.mainloop()
# end of module
Any suggestions? 有什么建议么?
Tkinter isn't designed to work this way. Tkinter并非以这种方式工作。 A complete program should only ever have a single instance of Tk
at one time. 一个完整的程序一次只能有一个Tk
实例。 You should create instances of Toplevel
in your sub-programs. 您应该在子程序中创建Toplevel
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.