向按钮添加信息

2024-03-28 11:12:43 发布

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

我期待着添加信息到这些按钮下面,所以当按下信息出现。。。。我一直在尝试我们的get()功能,这是正确的吗?我似乎无法让它运行,但下面是我的代码尝试:

submit = Button(frame, text="Enter", width=15, command=lambda: valueGET(E1.get(), E2.get())) 
submit.grid()

我的完整代码是:

 def raise_frame(frame):
        frame.tkraise()

    f1 = tk.Frame(root)
    f2 = tk.Frame(root)
    f3 = tk.Frame(root)
    f4 = tk.Frame(root)
    f5 = tk.Frame(root)

    for frame in (f1,f2,f3,f4,f5):
        frame.grid(row=0, column=0, sticky='news')

class MyButton(tk.Button):
    def __init__(self, *args, info=None, command=None, **kwargs):
        super().__init__(*args, **kwargs, command=self.callback)

        self.initialCommand = command
        self.info = info

    def display_info(self):
        # Display the information the way you want
        print(self.info)

    def callback(self):
        self.initialCommand()
        self.display_info()



    button1 = tk.Button(f1, text='Ya', command=lambda:raise_frame(f2)).pack()
    button2 = tk.Button(f1, text=Yb', command=lambda:raise_frame(f3)).pack()
    button3 = tk.Button(f1, text=Yc', command=lambda:raise_frame(f4)).pack()
    button4 = tk.Button(f1, text='Yd', command=lambda:raise_frame(f5)).pack()




    tk.Label(f2, text="Ya").pack()
button = tk.Button(root, text="Display info", command=lambda:print("Initial command"))
    button.pack()
    button.info = "Hello, world!"
        tk.Button(f2, text="HOME", command=lambda:raise_frame(f1)).pack()

        tk.Label(f3, text="Yb").pack()
button = tk.Button(root, text="Display info", command=lambda:print("Initial command"))
    button.pack()
    button.info = "Hello, world!"
        tk.Button(f3, text="HOME", command=lambda:raise_frame(f1)).pack()

        tk.Label(f4, text="Yc").pack()
button = tk.Button(root, text="Display info", command=lambda:print("Initial command"))
    button.pack()
    button.info = "Hello, world!"
        tk.Button(f4, text="HOME", command=lambda:raise_frame(f1)).pack()

        tk.Label(f5, text="Yd").pack()
button = tk.Button(root, text="Display info", command=lambda:print("Initial command"))
    button.pack()
    button.info = "Hello, world!"
        tk.Button(f5, text="HOME", command=lambda:raise_frame(f1)).pack()

    raise_frame(f1)
    root.mainloop()

if os.path.isfile(creds):
    Login()
else:
    Signup()

Tags: lambdatextselfinfobuttonrootframecommand
1条回答
网友
1楼 · 发布于 2024-03-28 11:12:43

据我所知,您希望存储与特定按钮相关的特定信息,这样当按下所述按钮时,信息就会显示在某个地方。你知道吗

您可以扩展Button类,以便使包装器能够保存该信息。你知道吗

import tkinter as tk

class MyButton(tk.Button):
    def __init__(self, *args, info=None, command=None, **kwargs):
        super().__init__(*args, **kwargs, command=self.callback)

        self.initialCommand = command
        self.info = info

    def display_info(self):
        # Display the information the way you want
        print(self.info)

    def callback(self):
        self.initialCommand()
        self.display_info()

这允许您随时通过设置ìnfo`属性来设置包装信息。你知道吗

在代码的开头添加这个类,并用MyButton实例替换所有Button实例。你知道吗

新按钮的行为应该与以前完全相同,只是当按下它们时,它们的info属性将以调用实例化时传递的命令后在display_info方法中定义的方式显示。你知道吗

您需要根据需要定义display_info(在控制台中打印,在标签中显示…)。你知道吗


下面是一个简短的例子:

root = tk.Tk()
button = MyButton(root, text="Display info", command=lambda:print("Initial command"))
button.pack()
button.info = "Hello, world!"

root.mainloop()

上面的代码显示一个带有单个按钮的根窗口。按下此按钮时,控制台中会打印Initial command,然后打印info属性,即"Hello, world!"。你知道吗

相关问题 更多 >