简体   繁体   English

Tkinter grid_forget在调用时不执行

[英]Tkinter grid_forget doesnt execute when called

I am experiencing something I really don't understand. 我遇到了我真的不明白的事情。 In the program, I pick a data file and then I want to hide the widgets in the window and load the data from file. 在程序中,我选择一个数据文件,然后将其隐藏在窗口中,然后从文件中加载数据。 But everything I tried results in first loading the file and then executing the rest... 但是我尝试的所有操作都会导致首先加载文件,然后执行其余的操作...

def LoadProject():
    old_project = FD.askopenfilename(filetypes=[("Data file","*.dat")], initialdir = "./Projects/")

    if old_project:
        napis.delete(TK.ALL)
        napis.grid_forget()
        button_new.grid_forget()
        button_load.grid_forget()

        data_file = open(old_project,"r")
        for line in data_file:
            line = line.replace("\n","")
            conv = line.split()
            data.append([float(conv[0]),int(conv[1]),float(conv[2]),float(conv[3]),float(conv[4])])

Everything that is before the for cycle is executed after the cycle finishes. for循环之前的所有操作都在循环完成之后执行。 Can anybody help me please? 有人可以帮我吗? I really don't understand this behavior. 我真的不明白这种行为。

It looks like you need to call the update method of the widgets you want to forget. 看来您需要调用要忘记的小部件的update方法。 This will flush the pending GUI changes. 这将刷新挂起的GUI更改。 From effbot.org : 来自effbot.org

update() 更新()

Processes all pending events, calls event callbacks, completes any pending geometry management, redraws widgets as necessary, and calls all pending idle tasks. 处理所有待处理的事件,调用事件回调,完成所有待处理的几何图形管理,根据需要重新绘制窗口小部件,并调用所有待处理的空闲任务。 This method should be used with care, since it may lead to really nasty race conditions if called from the wrong place (from within an event callback, for example, or from a function that can in any way be called from an event callback, etc.). 此方法应谨慎使用,因为如果从错误的位置(例如,从事件回调内部,或者从可以以任何方式从事件回调中调用的函数等)调用,则可能导致真正令人讨厌的竞争条件)。 When in doubt, use update_idletasks instead. 如有疑问,请改用update_idletasks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM