英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论