#导库
import tkinter as tk
import tempfile,time
from tkinter.messagebox import *
import sys
import math
import ttkbootstrap as ttk
def JieShu(): #结束事件
result = showwarning('警告', '确认关闭待办助手?')
sys.exit(0)
home.destroy()
#待办列表
daiban={123:-1,321:1}
def shuaxin():
home.update()
def ztsx(a):
daiban[a]=daiban[a]*-1
home.update()
# print(daiban)
home=ttk.Window(themename="journal")#想要切换主题,修改theme值即可,有以下这么多的主题,自己尝试吧:['vista', 'classic', 'cyborg', 'journal', 'darkly', 'flatly', 'clam', 'alt', 'solar', 'minty', 'litera', 'united', 'xpnative', 'pulse', 'cosmo', 'lumen', 'yeti', 'superhero', 'winnative', 'sandstone', 'default']
home.tk.call('wm', 'iconphoto', home._w, ttk.PhotoImage(file='tb.png')) #呜呜呜,难受,这东西设置图标有Bug
home.geometry('500x500')
home.attributes("-alpha",0.9)
home.title("待办助手v1.0")
home.resizable(width=True, height=True)
# home.overrideredirect(False)
def a(): #本来想搞自适应的.....太难了,算了算了
home.update()
return int(math.pow(home.winfo_height()+home.winfo_width(),1/2))
home.wm_attributes('-topmost',1)
# home.attributes('-toolwindow', True)
home.protocol("WM_DELETE_WINDOW", JieShu) #结束事件
# home.config(background = "white") #自从学费了ttkbootstrap库,就爽了~
top = ttk.Frame(home, width=home.winfo_height(), height=10, relief='groove', borderwidth=0)
top.pack()
frame=ttk.Frame(home, width=250, height=10, borderwidth=0)
for i in daiban:
frame2=ttk.Frame(frame, width=250, height=10, borderwidth=0)
if(daiban[i]==1):
aa=ttk.Text(frame2, height=1, width=21, font=('微软雅黑', 10, 'overstrike'))
aa.insert('0.0', i)
button = ttk.Button(frame2, text='未完成', command=ztsx(i))
else:
aa=ttk.Text(frame2, height=1, width=20, font=('微软雅黑', 10, 'bold'))
aa.insert('0.0', i)
button = ttk.Button(frame2, text='已完成', command=ztsx(i))
aa.configure(state="disabled")
aa.pack()
button.pack()
frame2.pack()
frame.pack()
home.bind('home.window_resize',shuaxin)
lb = ttk.Label(top, text="欢迎来到待办助手~=,font=('微软雅黑',int(a()*0.5-5)))
lb.pack()
home.mainloop()
“Devil组”引证GPT后的撰写:
在按下完成后,你需要更新相应的文本框以反映其已完成/未完成状态。你可以通过在ztsx函数中使用configure方法来实现这一点。当状态更改时,可以使用delete方法删除原始文本,然后使用insert方法将更新后的文本插入文本框中。
下面是修改后的代码片段:
def ztsx(a):
daiban[a]=daiban[a]*-1
for child in frame.winfo_children():
child.destroy()
for i in daiban:
frame2=ttk.Frame(frame, width=250, height=10, borderwidth=0)
if(daiban[i]==1):
aa=ttk.Text(frame2, height=1, width=21, font=('微软雅黑', 10, 'overstrike'))
aa.insert('0.0', i)
button = ttk.Button(frame2, text='未完成', command=lambda a=i: ztsx(a))
else:
aa=ttk.Text(frame2, height=1, width=20, font=('微软雅黑', 10, 'bold'))
aa.insert('0.0', i)
button = ttk.Button(frame2, text='已完成', command=lambda a=i: ztsx(a))
aa.configure(state="disabled")
aa.pack()
button.pack()
frame2.pack()
home.update()
ztsx函数遍历待办事项,并在更改其状态后删除原始框架中的所有小部件。然后,它再次创建框架,并使用更新后的状态创建新的小部件。请注意,我在绑定按钮命令时使用了lambda函数,以便将参数传递给函数。最后,使用home.update()更新主窗口,以便所有更改都立即反映在UI上。