刷新标签tkinter?

发布于 2025-02-10 14:28:02 字数 1310 浏览 2 评论 0原文

我想每次按下刷新按钮时刷新密码标签。 我尝试了.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

打小就很酷 2025-02-17 14:28:02
import tkinter as tk  # avoid star imports; this can lead to namespace pollution
import tkinter.messagebox
# other imports, etc.

# declare a StringVar to store the label text
password_var = tk.StringVar()


def get_all_passwords():
    with open('Passwords.txt') as f:
        password_Text = f.read()
        password_var.set(password_text)  # update 'password_var'
        # f.close() - you don't need this when using 'with'
        # see link below

“ context Managers and python with statement”

# add a 'textvariable' binding to your label
# the label text will update whenever the variable is set
All_passwords_label = tk.Label(
    window,
    text=password_Text,
    textvariable=password_var  # add this!
    foreground="black",
    background="white",
    width=25,
)

现在任何时候您调用get_all_all_passwords() all_passwords_label的标签文本应更新。
请注意,您根本不再需要all_passwords_text函数,也不需要使用Globals。

import tkinter as tk  # avoid star imports; this can lead to namespace pollution
import tkinter.messagebox
# other imports, etc.

# declare a StringVar to store the label text
password_var = tk.StringVar()


def get_all_passwords():
    with open('Passwords.txt') as f:
        password_Text = f.read()
        password_var.set(password_text)  # update 'password_var'
        # f.close() - you don't need this when using 'with'
        # see link below

"Context Managers and Python's with Statement"

# add a 'textvariable' binding to your label
# the label text will update whenever the variable is set
All_passwords_label = tk.Label(
    window,
    text=password_Text,
    textvariable=password_var  # add this!
    foreground="black",
    background="white",
    width=25,
)

Now any time you call get_all_passwords() the label text of All_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文