相关疑难解决方法(0)

如何将参数传递给Tkinter中的Button命令?

假设我Button在Python中使用Tkinter进行了以下操作:

import Tkinter as Tk
win = Tk.Toplevel()
frame = Tk.Frame(master=win).grid(row=1, column=1)
button = Tk.Button(master=frame, text='press', command=action)
Run Code Online (Sandbox Code Playgroud)

action当我按下按钮时调用该方法,但是如果我想将一些参数传递给方法action怎么办?

我尝试过以下代码:

button = Tk.Button(master=frame, text='press', command=action(someNumber))
Run Code Online (Sandbox Code Playgroud)

这只是立即调用方法,按下按钮什么都不做.

python arguments tkinter button python-3.x

143
推荐指数
7
解决办法
22万
查看次数

为什么在声明时执行Button参数"command"?

我的代码是:

from Tkinter import *

admin = Tk()
def button(an):
    print an
    print 'het'

b = Button(admin, text='as', command=button('hey'))
b.pack()
mainloop()
Run Code Online (Sandbox Code Playgroud)

按钮不起作用,在没有我的命令的情况下打印'hey'和'het'一次,然后,当我按下按钮时没有任何反应.

python tkinter

60
推荐指数
3
解决办法
2万
查看次数

标签 统计

python ×2

tkinter ×2

arguments ×1

button ×1

python-3.x ×1