[英]I want to put different command in tkinter Button (made by for())
from tkinter import *
window2 = Tk()
Var = ' '
store = ['a', 'b', 'c', 'd']
count = 3
for i in store:
if i != None:
chs_store = Button(window2, text = i).grid(row = count , column = 4)
count += 1
I made buttons and placed them on the frame like this.我制作了按钮并将它们放在这样的框架上。 In order to place buttons as same as the order of the list.
为了放置按钮与列表的顺序相同。 And now I want to put the different commands into each buttons.
现在我想将不同的命令放入每个按钮。
ex) If I click the Button that says 'a', I want Var = 'one'
, and if I click the Button 'b', I want Var = 'two'
. ex) 如果我点击显示“a”的按钮,我想要
Var = 'one'
,如果我点击按钮 'b',我想要Var = 'two'
。
How should I solve this我该如何解决这个问题
Put each command function in a list similar to what you're doing with store
将每个命令 function 放入类似于您对
store
所做的列表中
# define the functions you want your buttons to call up here, e.g.:
def func_a():
print('hello!')
# note: no parentheses () since you're not calling these functions here!
commands = [func_a, func_b, func_c, func_d]
store = ['a', 'b', 'c', 'd']
count = 3
for index, i in enumerate(store):
if i != None:
chs_store = Button(window2, text=i, command=commands[index])
# place your buttons on the grid in a separate line since 'grid' returns
# None, which means chs_store will have no value anyway
chs_store.grid(row=count, column=4)
count += 1
If you print out chs_store you will see None, since you have not stored the button object, but the result of the grid method.如果你打印出 chs_store 你会看到 None,因为你没有存储按钮 object,而是网格方法的结果。 Therefore, it is better to separate them.
因此,最好将它们分开。 In order not to make a counter, I used the
enumerate
function. I placed the functions that need to be attached to the buttons in a tuple.为了不做计数器,我使用了
enumerate
function。我将需要附加到按钮的功能放在一个元组中。
from tkinter import *
def fun_a():
global Var
Var = 'a'
print('Var = ', Var)
def fun_b():
global Var
Var = 'b'
print('Var = ', Var)
def fun_c():
global Var
Var = 'c'
print('Var = ', Var)
def fun_d():
global Var
Var = 'd'
print('Var = ', Var)
window2 = Tk()
Var = ' '
funs = (fun_a, fun_b, fun_c, fun_d)
store = ('a', 'b', 'c', 'd')
btns = []
for i, st in enumerate(store):
b = Button(window2, text=st, command=funs[i])
b.grid(row=i, column=4)
btns.append(b)
print(btns)
window2.mainloop()
[<tkinter.Button object .!button>, <tkinter.Button object .!button2>, <tkinter.Button object .!button3>, <tkinter.Button object .!button4>]
Var = a
Var = b
Var = c
Var = d
If you do not need a separate function for each button, then the code can be shortened.如果您不需要每个按钮单独的 function,则可以缩短代码。 I save the buttons in a list, since the variable b is overwritten at each iteration of the loop.
我将按钮保存在一个列表中,因为变量 b 在循环的每次迭代中都会被覆盖。
from tkinter import *
from functools import partial
def fun(s):
global Var
Var = s
print('Var = ', Var)
window2 = Tk()
Var = ' '
store = ('a', 'b', 'c', 'd')
btns = []
for i, st in enumerate(store):
b = Button(window2, text=st, command=partial(fun, st))
b.grid(row=i, column=4)
btns.append(b)
window2.mainloop()
Just based on the provided code, you can define another list for the position of the selected item in store
:只需根据提供的代码,您就可以为
store
中所选商品的position定义另一个列表:
store = ["a", "b", "c", "d"]
position = ["one", "two", "three", "four"]
Then print the corresponding position when a button is clicked.然后在点击某个按钮时打印对应的position 。
Below is the modified code:下面是修改后的代码:
from tkinter import *
window2 = Tk()
Var = ' '
store = ['a', 'b', 'c', 'd']
position = ['one', 'two', 'three', 'four']
def update_var(pos):
global Var
Var = pos
print(f"{Var=}")
count = 3
for i, pos in zip(store, position):
Button(window2, text=i, command=lambda pos=pos: update_var(pos)).grid(row=count, column=4)
count += 1
window2.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.