python tkinter按钮按一下继续执行_Python/Tkinter-将按钮状态写入文件以在下次启动时自动重新应用?...

我试图将按钮前景色的状态(由用户单击更改)保存到.txt文件中,以便下次运行时自动重新应用所述前景色。这是我的密码。。。toggle_color工作得很好,它还能够将颜色状态写入.txt文件。但是,它不会在启动时自动改变颜色。意思是,我可能在最后一部分搞错了。我做错了什么?

编辑:忘记调用.read()。但是

toggle_color(colors = ['white', '#0055C4'])

不是改变先前定义的公认方式。那么,我该如何改变toggle_color的颜色变量呢?

def toggle_color(last=[0]): #this is what toggles the fg color of the button

colors = ['#0055C4', 'white']

color = colors[last[0]]

last[0] = (last[0] + 1) % 2

savepass.config(fg=color)

with open('loaderstate.txt', 'w') as loader_file:

loader_file.write(savepass.cget('fg'))

savepasspic = PhotoImage(file="RememberMeBackground.png")

savepass=Button(root, font=('Arial Black',10),image=savepasspic, text=" remember me", compound=CENTER, command=toggle_color, activeforeground="#0055C4", activebackground="#303030", relief=GROOVE)

savepass.config(borderwidth=0, highlightthickness=0, width=120, height=18, fg='white', bg="grey")

savepass.place(x=105, y=287)

def blueoverall(): #changes the fg color of the button, and reverses toggle_color order

savepass.config(fg='#0055C4')

toggle_color(colors = ['white', '#0055C4'])

with open('loaderstate.txt', 'r') as loader_file: #should run automatically on startup and determine color of button fg

if loader_file.read == '#0055C4':

blueoverall()

if loader_file.read == 'white':

savepass.config(fg='white')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值