如何在tkinter中创建一个带有名称参数/属性的按钮命令?

huangapple go评论62阅读模式
英文:

How to make a button with a command with the parameter/attribute of its name with tkinter?

问题

from tkinter import *

def openfile(name):
    print(name)

for i in l:
    if num == 2:
        num = 0
        y = y + 100
        x = 100
        print(y)
        print(x)
        print(num)

    bt = Button(second_frame, text=i, command=lambda i=i: openfile(i)).grid(row=y, column=x, pady=10, padx=10)
    num = num + 1
    x = x + 100
    print(num)
英文:

How to make a button with a command with the parameter/attribute of its name with tkinter?

from tkinter import *

def openfile(name):
    print(name)

for i in l:
    if num == 2:
        num = 0
        y = y+100
        x = 100
        print(y)
        print(x)
        print(num)

    bt = Button(second_frame, text=i, command=lambda: openfile(i)).grid(row=y, column=x, pady=10, padx=10)
    num = num + 1
    x = x + 100
    print(num)

I want to set the button command to be open file then its name as an attribute. I don't know how to get the buttons name on the same line.

NOTE:
The button name differs from each button

答案1

得分: 0

像这样吗?

for i in range(5):
   btn_name=str(i)
   btn_obj=Button(second_frame,text=btn_name,command=lambda name=btn_name:openfile(name))
英文:

You mean something like this ?

for i in range(5):
   btn_name=str(i)
   btn_obj=Button(second_frame,text=btn_name,command=lambda name=btn_name:openfile(name))

huangapple
  • 本文由 发表于 2023年2月6日 19:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定