如何在 tkinter 中以编程方式将相同的操作关联到一组按钮?
How to associate the same operation to a set of buttons programmatically in tkinter?
我一直在从事一个 Python/Tkinter 项目,该项目需要从如下列表中以编程方式创建按钮。
当按下某个按钮时,我希望那个按钮变成 "sunken"
直到按下另一个按钮,在那个阶段那个按钮会变成 "sunken"
,第一次点击的按钮会变成'normal'
。
到目前为止,我无法弄清楚如何在不必为每个按钮单独编写代码的情况下执行此操作。
理想情况下,relief
将在 press()
函数中设置。
import tkinter
window = tkinter.Tk()
window.title("Practice UI")
window.grid()
numbers = ["1","2","3","4","5","6","7","8","9"]
def buttonCreator(labels):
n = 0
button = []
for x in range(0,3):
for y in range(0,3):
if n<=len(labels)-1:
button.append(tkinter.Button(window, text = labels[n], command = lambda x = labels[n]:press(x)))
button[n].grid(row = x, column = y)
n +=1
def press(value):
print(value)
buttonCreator(numbers)
window.mainloop()
您正在描述一组单选按钮的行为,因此您应该使用它们。
单选按钮有一个 indicatoron
属性
Normally a radiobutton displays its indicator. If you set this option to zero, the indicator disappears, and the entire widget becomes a “push-push” button that looks raised when it is cleared and sunken when it is set. You may want to increase the borderwidth value to make it easier to see the state of such a control.
因此,当设置为零时,它会精确地满足您的要求。
举个例子,我将创建两个具有您想要的反应的按钮:
from Tkinter import *
win = Tk()
def press1():
button1["relief"] = "sunken"
button2["relief"] = "normal"
def press2():
button1["relief"] = "normal"
button2["relief"] = "sunken"
button1 = Button(win, text="Button 1", command=press1)
button2 = Button(win, text="Button 2", command=press2)
button1.pack()
button2.pack()
button1.grid(row=1, column=1)
button2.grid(row=1, column=2)
win.mainloop()
应该可以。第二种方式:
from Tkinter import *
win = Tk()
def press(buttonnumber):
if buttonnumber==1:
button1["relief"] = "sunken"
button2["relief"] = "normal"
elif buttonnumber==2:
button1["relief"] = "normal"
button2["relief"] = "sunken"
else:
raise Exception("No Button Number \"%d\"" % buttonnumber)
button1 = Button(win, text="Button 1", command=lambda: press(1))
button2 = Button(win, text="Button 2", command=lambda: press(2))
button1.pack()
button2.pack()
button1.grid(row=1, column=1)
button2.grid(row=1, column=2)
win.mainloop()
您可以简单地 return button
并使用它来访问您在 press()
:
中的按钮
import tkinter
window = tkinter.Tk()
window.grid()
# changed to integers so we can loop through the
# values in press() and use them as indices
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def buttonCreator(labels):
n = 0
button = []
for x in range(0, 3):
for y in range(0, 3):
if n <= len(labels) - 1:
button.append(tkinter.Button(window, text=labels[n],
command=lambda x=labels[n]:press(x)))
button[n].grid(row=x, column=y)
n += 1
return button # added a return statement
def press(value):
for x in numbers:
# index is x-1 because 1 is in button[0], etc
button[x-1]['relief'] = 'sunken' if x == value else 'raised'
button = buttonCreator(numbers)
window.mainloop()
我一直在从事一个 Python/Tkinter 项目,该项目需要从如下列表中以编程方式创建按钮。
当按下某个按钮时,我希望那个按钮变成 "sunken"
直到按下另一个按钮,在那个阶段那个按钮会变成 "sunken"
,第一次点击的按钮会变成'normal'
。
到目前为止,我无法弄清楚如何在不必为每个按钮单独编写代码的情况下执行此操作。
理想情况下,relief
将在 press()
函数中设置。
import tkinter
window = tkinter.Tk()
window.title("Practice UI")
window.grid()
numbers = ["1","2","3","4","5","6","7","8","9"]
def buttonCreator(labels):
n = 0
button = []
for x in range(0,3):
for y in range(0,3):
if n<=len(labels)-1:
button.append(tkinter.Button(window, text = labels[n], command = lambda x = labels[n]:press(x)))
button[n].grid(row = x, column = y)
n +=1
def press(value):
print(value)
buttonCreator(numbers)
window.mainloop()
您正在描述一组单选按钮的行为,因此您应该使用它们。
单选按钮有一个 indicatoron
属性
Normally a radiobutton displays its indicator. If you set this option to zero, the indicator disappears, and the entire widget becomes a “push-push” button that looks raised when it is cleared and sunken when it is set. You may want to increase the borderwidth value to make it easier to see the state of such a control.
因此,当设置为零时,它会精确地满足您的要求。
举个例子,我将创建两个具有您想要的反应的按钮:
from Tkinter import *
win = Tk()
def press1():
button1["relief"] = "sunken"
button2["relief"] = "normal"
def press2():
button1["relief"] = "normal"
button2["relief"] = "sunken"
button1 = Button(win, text="Button 1", command=press1)
button2 = Button(win, text="Button 2", command=press2)
button1.pack()
button2.pack()
button1.grid(row=1, column=1)
button2.grid(row=1, column=2)
win.mainloop()
应该可以。第二种方式:
from Tkinter import *
win = Tk()
def press(buttonnumber):
if buttonnumber==1:
button1["relief"] = "sunken"
button2["relief"] = "normal"
elif buttonnumber==2:
button1["relief"] = "normal"
button2["relief"] = "sunken"
else:
raise Exception("No Button Number \"%d\"" % buttonnumber)
button1 = Button(win, text="Button 1", command=lambda: press(1))
button2 = Button(win, text="Button 2", command=lambda: press(2))
button1.pack()
button2.pack()
button1.grid(row=1, column=1)
button2.grid(row=1, column=2)
win.mainloop()
您可以简单地 return button
并使用它来访问您在 press()
:
import tkinter
window = tkinter.Tk()
window.grid()
# changed to integers so we can loop through the
# values in press() and use them as indices
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def buttonCreator(labels):
n = 0
button = []
for x in range(0, 3):
for y in range(0, 3):
if n <= len(labels) - 1:
button.append(tkinter.Button(window, text=labels[n],
command=lambda x=labels[n]:press(x)))
button[n].grid(row=x, column=y)
n += 1
return button # added a return statement
def press(value):
for x in numbers:
# index is x-1 because 1 is in button[0], etc
button[x-1]['relief'] = 'sunken' if x == value else 'raised'
button = buttonCreator(numbers)
window.mainloop()