tkinter entry widget not updating

ぃ、小莉子 提交于 2021-01-29 06:31:41

问题


I've searched everywhere on the web but unfortunately no where did I find an answer to this question:

after setting a tkinter Entry() widget's textvariable to a textvariable.

the text variable does not update after I have typed text into the entry.

code below:

def saveFileName(self):
    if(self.save_file_name != ""):
        self.window.destroy()
        self.name_not_set = False
        print("saving...")
    else:
        print("notsaving...entry empty")
        pass
def cancel(self):
    self.name_not_set = False
    self.exit = True
    self.window.destroy()
    print("exiting...")
def askForFilename(self):
    self.window = tk.Tk()
    self.window.wm_title("enter a file name")
    label = Label(self.window,text="please enter a file name:").pack(side="top")
    entry = Entry(self.window,textvariable=self.save_file_name).pack()
    save = Button(self.window,text="save",command=self.saveFileName).pack()
    cancel = Button(self.window,text="cancel",command=self.cancel).pack()
    self.window.mainloop()

The necessary variables have been defined and these methods are part of a class which is a tk.TK() instance.

this problem is very bothersome :( very sad :(

Thank you and merry christmas in advance!


回答1:


A textvariable associated with an Entry should be a StringVar(). I don't se any such declaration in your code.

self.save_file_name = StringVar()

To set and get the value of a StringVar() you must use the set() or get() method, eg.

def saveFileName(self):
    if(self.save_file_name.get() != ""):
        self.window.destroy()
        # etc, etc.

Also, don't create more than one instance of Tk() as in:

def askForFilename(self):
    self.window = tk.Tk()

Use Toplevel() instead. Or even better: use the tkinter filedialogs.



来源:https://stackoverflow.com/questions/53366916/tkinter-entry-widget-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!