有没有更有效的方法更新tkinter上的标签?

2024-10-02 08:18:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码。它按我所希望的方式工作,但我总是被告知使用全局变量是糟糕的编码实践,它们可能会导致问题,尽管我不知道如何在不使用它们的情况下更改标签。感谢您的帮助

import tkinter as tk
from tkinter import filedialog, Text
import os

status = 'Start'
startStopBG = 'light green'

def main():
    root = configure_screen()
    root.mainloop()


def configure_screen():
    root = tk.Tk()
    root.title('APP')
    root.config(bg='snow3')
    root.minsize(700, 700)

    browse_button = tk.Button(root, text='Browse', width='10', command=browse)
    browse_button.place(x=605, y=10)

    global text
    text = tk.Text(root, height=1.3, width=73)
    text.insert(tk.END, 'Enter Path to Storage HERE')
    text.place(x=10, y=13)

    global start_stop
    start_stop = tk.Button(root, height=1, width=12, text=status, bg=startStopBG,
                           font=('Helvetica', '40'), command=start_scanning)
    start_stop.pack(pady=50)
    return root


def browse():
    path = filedialog.askdirectory(initialdir='/', title='Select where you want to save your file')
    text.delete('1.0', tk.END)
    text.insert(tk.END, path)

def start_scanning():
    global status
    global startStopBG
    global start_stop
    if status == 'Start':
        status = 'Stop'
        startStopBG = 'red'
    else:
        status = 'Start'
        startStopBG = 'light green'
    start_stop.config(text=status, bg=startStopBG)


if __name__ == '__main__':
    main()

Tags: textimportmaindefstatusrootwidthglobal
2条回答

首先,您可以在主窗口中使用类,而不是使用全局变量,您可以使用类变量。第二,我建议您使用tkinter变量来存储来自widget的重要数据,因为路径和状态。例如,如果使用text=tk.StringVar(),则可以使用text.set('value')text.get()从文本中设置或获取值。Tkinter变量是对象,如果您在main中定义了一个对象,则可以在函数中作为全局变量访问它,而无需使用global。但是,在您的代码中,要将text用作StringVar,您应该将Text小部件更改为Entry小部件,这更合适,因为path是单个条目值而不是文本。同样,您可以将start_stop按钮更改为Checkutton,这将使颜色更改变得不必要,因为您可以为backgroundselectcolor定义颜色

下面的代码包括我在此建议的所有更改:

将tkinter作为tk导入 从tkinter导入文件对话框,文本 导入操作系统

class Main(tk.Tk):
    def __init__(self):
        super(Main, self).__init__()
        self.title('APP')
        self.config(bg='snow3')
        self.minsize(700, 700)

        self.status = tk.IntVar()
        self.text = tk.StringVar(self, value='Enter Path to Storage HERE')

        browse_button = tk.Button(self, text='Browse', width='10', 
                                  command=self.browse)
        browse_button.place(x=605, y=10)

        tk.Entry(self, width=73, textvariable=self.text).place(x=10, y=13)

        self.start_stop = tk.Checkbutton(self, height=1, width=12, text="start", 
                                    font=('Helvetica', '40'), indicator=False, 
                                    bg='light green', selectcolor='red', 
                                    variable=self.status, command=self.start_scanning)
        self.start_stop.pack(pady=50)

    def browse(self):
        path = filedialog.askdirectory(initialdir='/', title='Select where you want to save your file')
        self.text.set(path)

    def start_scanning(self):
        if self.status.get():
            self.start_stop.config(text='stop')
        else:
            self.start_stop.config(text='start')


if __name__ == '__main__':
    Main().mainloop()

据我所知,你想更换标签

试试这个:

import tkinter as tk

def main():

    def change_label_text():
        mylabel.config(text="yeee My Text has Been changed")

    def change_button_text():
        mybutton.config(text="yee Button text has been changed")

    root = tk.Tk()
    root.title('Change Label')
    root.config(bg='snow3')
    root.geometry('400x300')

    mybutton = tk.Button(root, text='Press Me To Change Button Text', command=change_button_text)
    mybutton.pack(side='top')

    mylabel = tk.Label(root, text='Press The Button Above To Change My text')
    mylabel.pack(side='bottom')

    mybutton2 = tk.Button(root, text="Press me", command=change_label_text)
    mybutton2.pack(side='bottom')
    root.mainloop()

main()

通过在主循环中创建函数,您无需执行全局操作和所有操作

相关问题 更多 >

    热门问题