代码之家  ›  专栏  ›  技术社区  ›  Sabito stands with Ukraine ismail PANDA

如何在Tkinter中完成流程后再次启用元素

  •  0
  • Sabito stands with Ukraine ismail PANDA  · 技术社区  · 4 年前

    from tkinter import *
    from tkinter import ttk
    
    
    root = Tk()
    note = ttk.Notebook(root)
    
    tab0 = ttk.Frame(note)
    tab1 = ttk.Frame(note)
    
    note.add(tab0)
    note.add(tab1, state="disabled") # tab disabled
    note.pack(expand = 1, fill = "both")
    
    # <----------------tab0------------------>
    canvas_home = Canvas(tab0,bg='#398FFF')
    canvas_home.pack(expand=True,fill=BOTH)
    
    # In my case create_file is being imported...
    def create_file():
        import time
        time.sleep(100) # assume a file is being created
    button = Button(canvas_home, text="Create file",command=create_file)
    button.grid()
    # <----------------tab1------------------>
    import os
    if os.path.exists('filename'):
        note.tab(0, state="normal") # tab enabled
    
    if __name__ == "__main__":
        root.mainloop()
    
    0 回复  |  直到 4 年前
        1
  •  2
  •   jizhihaoSAMA    4 年前

    一些错误:你没有把你的按钮放在你的相框里。您想启用吗 tab1 但你能 tab0

    假设您想要创建一个名为 create_text ,你可以用 .after() 检查文件是否存在。

    from tkinter import *
    from tkinter import ttk
    from tkinter import filedialog
    import os
    
    
    def create_file():
        create_path = filedialog.asksaveasfilename()
        if create_path:
            with open(create_path, "w") as f: # create a empty text
                pass
    
    def check_file():
        if os.path.exists('create_text.txt'): # check whether it exists
            note.tab(1, state="normal")
        else:
            root.after(100, check_file)
    
    
    root = Tk()
    note = ttk.Notebook(root)
    
    tab0 = ttk.Frame(note)
    tab1 = ttk.Frame(note)
    
    note.add(tab0)
    note.add(tab1, state="disabled")  # tab disabled
    note.pack(expand=1, fill="both")
    
    # <----------------tab0------------------>
    canvas_home = Canvas(tab0, bg='#398FFF')
    canvas_home.pack(expand=True, fill=BOTH)
    
    button = Button(canvas_home, text="Create file", command=create_file)
    button.grid()
    
    root.after(100, check_file)
    
    if __name__ == "__main__":
        root.mainloop()