在运行时单击时如何更改按钮的颜色? [英] How can I change the colour of a button when clicked at runtime?

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

问题描述

button1=Button(root,text="A1",width=8).grid(row=0,column=0)
button2=Button(root,text="A2",width=8).grid(row=0,column=1)

label1=Label(root,text="       ",padx=20).grid(row=0,column=2)

button22=Button(root,text="A3",width=8).grid(row=0,column=3,sticky='E')
button23=Button(root,text="A4",width=8).grid(row=0,column=4,sticky='E')

我正在尝试为学校项目制作座位安排系统.我有一个问题:单击按钮后如何更改按钮的颜色?单击该按钮后,我想更改已预订和可用座位的颜色.

I'm trying to make seat arrangement system for a school project. I have an issue: how can I change the colour of the button once it is clicked? I want to change the colour of the booked and available seats once I click on that button.

推荐答案

如果只希望在单击时更改按钮的颜色,则需要在该按钮上使用 .config()方法按钮小部件.

If you just wish to change the color of a button when clicked, you need to use the .config() method on that button widget.

例如,如果定义的按钮类似

for example, if a button is defined like

aButton = tk.Button(root, text='change my color').pack()

然后更改颜色(或几乎与该小部件相关的所有内容,如文本,命令或其他任何东西),调用该方法

then to change the color (or pretty much everything related to that widget like text, command or whatever) call the method

aButton.configure(bg='#f0f', fg='#fff') # change to your required colors, bg is background, fg is foreground.

也可以使用

OR .config()(这两种方法的作用完全相同)

OR .config() can be also be used (these 2 methods do exactly the same)

aButton.config(bg='#f0f', fg='#fff')

现在您将如何知道何时单击按钮.最简单直观的方法是定义功能并将其连接(或 )到按钮.现在,您要如何操作完全取决于用户的偏好.有些人喜欢为所有按钮创建单独的不同功能,而有些人只喜欢创建一个.

Now how will you know when a button is clicked or not. The simplest and intuitive way is to define functions and connect (or bind) them to buttons. Now how you wanna do that is totally up to the user's preferences. Some prefer to create separate different functions for all buttons, some only like to create one.

但是对于您的情况,因为除了更改颜色之外您不需要执行任何其他操作,因此单个功能就足够了.重要在下面的示例代码中,我使用了 lambda 函数,这是一种特殊类型的函数,不需要单独定义.但是,那绝不是必需的

For your case though, as you don't need to do anything additional than changing colors then single function is enough. Important In the example code below, I have used lambda functions, a special type of functions, which don't need to be defined separately. That however, by no means, is necessary

from tkinter import *  # I don't recommend using global import. better use "import tkinter as tk"

root = Tk()

button1=Button(root,text="A1",width=8, command=lambda: button1.config(bg='#f00'))
button1.grid(row=0,column=0)

button2=Button(root,text="A2",width=8, command=lambda: button2.config(bg='#f00'))
button2.grid(row=0,column=1)

Label(root,text=" ",padx=20).grid(row=0,column=2)

button22=Button(root,text="A3",width=8, command=lambda: button22.config(bg='#f00'))
button22.grid(row=0,column=3,sticky='E')

button23=Button(root,text="A4",width=8, command=lambda: button23.config(bg='#f00'))
button23.grid(row=0,column=4,sticky='E')

root.mainloop()

使用功能

来自tkinter import的

Using functions

from tkinter import *  # I don't recommend using global import. better use "import tkinter as tk"


def changeColor(btn):
    # Use your own highlight background argument here instead of bg
    btn.configure(bg='#f00')


root = Tk()

button1=Button(root,text="A1",width=8, command=lambda: changeColor(button1))
button1.grid(row=0,column=0)

button2=Button(root,text="A2",width=8, command=lambda: changeColor(button2))
button2.grid(row=0,column=1)

Label(root,text=" ",padx=20).grid(row=0,column=2)

button22=Button(root,text="A3",width=8, command=lambda: changeColor(button22))
button22.grid(row=0,column=3,sticky='E')

button23=Button(root,text="A4",width=8, command=lambda: changeColor(button23))
button23.grid(row=0,column=4,sticky='E')

root.mainloop()

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

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