一些错误:你没有把你的按钮放在你的相框里。您想启用吗
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()