单击时更改按钮外观 [英] Changing Button Appearance On Click

查看:51
本文介绍了单击时更改按钮外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GUI应用程序中,我为给定列表中的每个项目创建一个按钮.当按下按钮时,该按钮的背景变为绿色,并且显示为下沉".其他按钮显示为凸起并具有默认颜色.我的问题是只有创建的最后一个按钮会更改颜色和外观.我相信这是因为它包含回调函数.

In my GUI application I create a button for every item in a given list. When a button is pressed the background of said button turns green and it appears as "sunken". The other buttons appear raised and with the default color. My issue is that only the last button created changes color and appearance. I believe this is because it contains the callback function.

我希望在按下初始按钮之后,当用户按下另一个按钮时,新按钮变为绿色并显示为凹陷,而前一个按钮变为凸起状态和默认颜色.我的猜测是,我需要创建所有已创建按钮的列表,并在回调函数中对其应用逻辑以实现所需的行为.但是怎么办?

I desire that after the initial button press, when a user presses a different button that the new button turns green and appears sunken and the previous button becomes raised and the default color. My guess is that I need to create a list of all created buttons and apply logic to it in the callback function for my desired behavior. But how so?

# ---callback functions---
def select_button():
    btn.config(bg='green', activebackground='green', relief=SUNKEN)

# ---main---
# List of buttons to be created
list = ['A', 'B', 'C']
buttons = []

# Create buttons
for i, name in enumerate(list, 2):
    btn = Button(root, text=name, command=select_button)
    btn.grid(row=i, column=0, sticky=W)
    buttons.append(btn)

推荐答案

在Stackoverflow上已经有很多次了,所以我不知道是否应该再写一次.

It was so many times on Stackoverflow so I don't know if I should write it again.

按钮具有 command = 来分配功能,但可以分配不带参数的功能.如果需要带参数,则必须使用 lambda .我参考按钮分配功能,以便功能可以使用正确的按钮并进行更改.

Button has command= to assign function but it can assign function without arguments. If you need with arguments you have to use lambda. I assign function with reference to button so function can use correct button and change it.

lambda 需要 arg = btn select_button(arg),因为直接select_button(btn)将在所有功能中使用最后一个按钮.

lambda in for-loop needs arg=btn and select_button(arg) because direct select_button(btn) will use last button in all functions.

关于将上一个按钮更改为原始颜色,您可以使用变量记住先前单击的按钮,然后可以轻松地将其更改为颜色.

As for changing previous button to original color you can use variable to remember previusly clicked button and then you can easily change it color.

问题可能是找到按钮的原始颜色,所以我从新单击的按钮中复制了它.

Problem can be to find oryginal color of button so I copy it from new clicked button.

import tkinter as tk

# --- functions ---

def select_button(widget):
    global previously_clicked

    if previously_clicked:
        previously_clicked['bg'] = widget['bg']
        previously_clicked['activebackground'] = widget['activebackground']
        previously_clicked['relief'] = widget['relief']

    widget['bg'] = 'green'
    widget['activebackground'] = 'green'
    widget['relief'] = 'sunken'

    previously_clicked = widget

# --- main ---

names = ['Button A', 'Button B', 'Button C']

root = tk.Tk()

previously_clicked = None

for i, name in enumerate(names, 2):
    btn = tk.Button(root, text=name)

    btn.config(command=lambda arg=btn:select_button(arg))
    #btn['command'] = lambda arg=btn:select_button(arg)

    btn.grid(row=i, column=0, sticky='w')

root.mainloop()


编辑:您还可以使用带有某些选项的 Radiobutton 来执行相同的操作-并且没有功能:


you can also use Radiobutton with some options to do the same - and without function:

请参阅: http://effbot.org/tkinterbook/radiobutton.htm

import tkinter as tk

# --- functions ---

def select_button():
    print('value:', v.get())  # selected value

# --- main ---

names = ['Button A', 'Button B', 'Button C']

root = tk.Tk()

v = tk.IntVar()  # variable for selected value

for i, name in enumerate(names, 2):
    btn = tk.Radiobutton(root, text=name, variable=v, value=i)  # assign variable and value

    btn['indicatoron'] = 0  # display button instead of radiobutton
    btn['selectcolor'] = 'green' # color after selection

    btn['command'] = select_button  # function without variables

    btn.grid(row=i, column=0, sticky='w', ipadx=5, ipady=5)

root.mainloop()

这篇关于单击时更改按钮外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆