刷新标签tkinter?
我想每次按下刷新按钮时刷新密码标签。 我尝试了.config
,但它不起作用。
import random
import string
import tkinter.messagebox
from tkinter import *
def get_all_passwords():
with open('Passwords.txt') as f:
global password_Text
password_Text = f.read()
f.close()
def main_window():
def all_passwords_text():
global password_Text
All_passwords_label.config(text=password_Text)
All_passwords_label = tkinter.Label(
window,
text=password_Text,
foreground="black",
background="white",
width=25,
)
password_label = tkinter.Label(
text=password,
foreground="white",
background="black",
width=20,
)
save_button = tk.Button(
text='save',
width=10,
cursor='hand1',
bg='light gray',
command=lambda:[write_password_to_txt(), All_passwords_label, window.update()]
)
refresh_button = tk.Button(
text='Refresh',
width=20,
bg="white",
fg="black",
command=all_passwords_text
)
All_passwords_label.pack(side='right')
password_label.pack()
safe_entry.pack()
save_button.pack()
refresh_button.pack()
window.mainloop()
random_password()
main_window()
I want to refresh the the password Label every time the refresh button is pressed.
I tried the .config
but it doesn't work.
import random
import string
import tkinter.messagebox
from tkinter import *
def get_all_passwords():
with open('Passwords.txt') as f:
global password_Text
password_Text = f.read()
f.close()
def main_window():
def all_passwords_text():
global password_Text
All_passwords_label.config(text=password_Text)
All_passwords_label = tkinter.Label(
window,
text=password_Text,
foreground="black",
background="white",
width=25,
)
password_label = tkinter.Label(
text=password,
foreground="white",
background="black",
width=20,
)
save_button = tk.Button(
text='save',
width=10,
cursor='hand1',
bg='light gray',
command=lambda:[write_password_to_txt(), All_passwords_label, window.update()]
)
refresh_button = tk.Button(
text='Refresh',
width=20,
bg="white",
fg="black",
command=all_passwords_text
)
All_passwords_label.pack(side='right')
password_label.pack()
safe_entry.pack()
save_button.pack()
refresh_button.pack()
window.mainloop()
random_password()
main_window()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“ context Managers and python with statement”
现在任何时候您调用
get_all_all_passwords()
all_passwords_label
的标签文本应更新。请注意,您根本不再需要
all_passwords_text
函数,也不需要使用Globals。"Context Managers and Python's with Statement"
Now any time you call
get_all_passwords()
the label text ofAll_passwords_label
should update.Note that you no longer need the
all_passwords_text
function at all, and you don't need to use globals either.