簡體   English   中英

輸入字段未使用用戶定義的模塊進行更新

[英]The Entry fields are not being updated using user-defined modules

我在python3中有一個主程序,該主程序通過模塊調用子程序。 效果很好,除了在模塊中其他位置進行更改時,我的模塊不會更新Entry字段。 模塊顯示值已放置在正確的位置,但窗口未更新。 這是主程序的示例:

# 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

這就是被調用的模塊(存儲為“ 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

有什么建議么?

Tkinter並非以這種方式工作。 一個完整的程序一次只能有一個Tk實例。 您應該在子程序中創建Toplevel實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM