【问题标题】:tkinter: lambda multiple Buttons to a Label?tkinter:lambda多个按钮到一个标签?
【发布时间】:2018-01-07 22:21:58
【问题描述】:

描述:我希望能够单击一个按钮并将其值发送到标签小部件(lab_1),我知道你必须使用一个按钮 command=lambda: lab_1.configure(text=0) 但如何当我使用for 循环在嵌入列表中创建所有按钮时,您可以将值打印到标签上。截至目前,所有按钮都有名为 key_press 的变量。

简化问题:

  • 如何将所有值写入我的标签 (lab_1)?

  • 如何使用for 循环,以便每次单击该按钮时都会将其值显示给Label


import tkinter as tk

root = tk.Tk()
# Change the original tkinter icon
root.iconbitmap('C:\\Users\\bmxfi\Downloads\images.ico')
# Set the title of the window
root.title('Calculator')
# Change the geometry of the window
root.geometry('300x450+750+150')
# Setting Minimum and Maximum window width and height
root.minsize(width=300, height=450)
root.maxsize(width=300, height=450)
# Create Rows and Columns to display widgets
root.rowconfigure(0, weight=100)
root.rowconfigure(1, weight=100)
root.rowconfigure(2, weight=100)
# Columns start here:
root.columnconfigure(0, weight=100)
root.columnconfigure(1, weight=100)
root.columnconfigure(2, weight=100)
# Configuring the Main window named: root
root.configure(bg='#353535')
# Creating the main label that will display the calculated results
lab_1 = tk.Label(root, width=40)
lab_1.grid(row=0, column=1, columnspan=1, sticky='we')
lab_1.configure(bg='#545454', font=('Computer Modern', 25, 'bold'),
                fg='White', justify='right')
# Main Calculator Layout
# List Box for a calculator layout
box_1 = tk.Listbox(root)
box_1.grid(row=1, column=1, rowspan=2)
box_1.configure(bg='#353535', relief='flat')
# Button Layout
calculator_layout = [[(7, 1), (8, 1), (9, 1), ('(', 1), (')', 1)],
                     [(4, 1), (5, 1), (6, 1), ('×', 1), ('÷', 1)],
                     [(1, 1), (2, 1), (3, 1), ('+', 1), ('-', 1)],
                     [(0, 1), ('±', 1), ('.', 1), ('-', 1), ('=', 1)]]

# Iterating through my list to get a set of buttons
row = 0
for i in calculator_layout:
    col = 0
    for a in i:
        key_press = tk.Button(box_1, text=a[0])
        key_press.grid(row=row, column=col, ipadx=10, ipady=8, sticky='we')
        key_press.configure(background='#545454', fg='white',
                            font=('Computer Modern', 10),
                            activebackground='red', state='normal')
        col += a[1]
    row += 1

root.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter lambda


    【解决方案1】:

    您可以通过给lambda 每个Button 一个带有默认值的参数来实现。这样做不需要任何额外的for 循环(它在创建Buttons 的那个循环中完成)。

    如何做到这一点在下面的# CHANGED 评论中显示。每个 lambda 函数定义的其余代码都重新配置名为 Labeltext 选项,方法是调用其 configure() 方法并将 text 选项的当前内容传递给附加到它的关联Button 的符号。

    ...
    
    # Iterating through my list to get a set of buttons
    row = 0
    for i in calculator_layout:
        col = 0
        for a in i:
            key_press = tk.Button(box_1, text=a[0])
            key_press.grid(row=row, column=col, ipadx=10, ipady=8, sticky='we')
            key_press.configure(background='#545454', fg='white',
                                font=('Computer Modern', 10),
                                activebackground='red', state='normal',
                                # CHANGED.
                                command=lambda sym=str(a[0]):
                                    lab_1.configure(text=lab_1.cget('text')+sym))
            col += a[1]
        row += 1
    
    root.mainloop()
    

    【讨论】:

    • 非常感谢?我为此苦苦挣扎了好几天。
    • @Braiden:很高兴听到这个消息。做这样的事情有时称为“extra arguments trick”,并且在处理tkinter 时在几种不同的情况下很有用。
    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2019-10-17
    • 2011-11-12
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多