简体   繁体   English

我想在 tkinter 按钮中放置不同的命令(由 for() 制作)

[英]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.

相关问题 Python 3 tkinter按钮命令在不同的类中 - Python 3 tkinter button command inside different class 我想要一个按钮来执行双重命令一个命令将终止我当前的 window 另一个命令将在 tkinter 中打开一个新的 window - I want a single button to do double command one command will terminate my present window and another command will open a new window in tkinter 在不同的窗口 tkinter 上按下按钮后如何输入文本 - how do i get put a text after a button is pressed on a different window tkinter 我可以在没有按钮的情况下在 Tkinter 中运行命令吗 - can I run a command in Tkinter without Button 我对tkinter按钮命令功能有疑问 - I had problem with tkinter button command function 我想在 tkinter 中制作的记事本左侧添加边距,但我不知道该怎么做? - I want to add margin to the left side of notepad I made in tkinter and I don't know how to do it? 每当我单击 tkinter 中的按钮时,我都想增加一个变量 - I want to increase a variable whenever I click a button in tkinter tkinter:使用相同的框架但有不同的命令按钮? - tkinter: Using same frame but having different command button? 如何将几个 if 语句放入 Tkinter 按钮 - How can I put several if statements into a Tkinter Button 如何将按钮放入 tkinter 窗口/pyautogui - How can I put a button into a tkinter window/ pyautogui
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM