[英]How to (re)enable tkinter ttk Scale widget after it has been disabled?
[英]How to enable an element again after a process has been completed in Tkinter
所以我有一个 2 页的笔记本。 最初 tab1 是禁用的。 单击 tab0 中的按钮会触发创建文件的外部过程。 只有创建此文件后,才应再次启用 tab1。我无法编辑创建此文件的代码,创建它需要一些时间,因此与此同时,我的代码应不断检查它是否已创建,然后启用选项卡...我该怎么做是 tkinter?
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()
一些错误:你没有把你的按钮放在你的框架中。你想启用tab1
但你启用tab0
假设你想创建一个名为create_text
的 txt 文件,你可以使用.after()
来检查文件是否存在。
在本例中,它将每 0.1 秒检查一次文件:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.