【问题标题】:Python Tkinter Button ActionPython Tkinter 按钮操作
【发布时间】:2014-06-06 09:32:59
【问题描述】:

我有这个代码:

#!/usr/bin/python

import Tkinter
from tkFileDialog import askopenfilename
import tkMessageBox

root = Tkinter.Tk()

def getFileName():
  # show an "Open" dialog box.
  filename = askopenfilename(filetypes = [('Text files', '*.txt'),('All files','*')])

btnIco = Tkinter.Button(root, text="Icon", command=getFileName())
btnIco.pack()

root.mainloop()

我打算在单击按钮时运行函数getFileName。但是,该函数在代码运行时运行,而按钮在单击时不执行任何操作。你能指出什么是错的吗?

【问题讨论】:

标签: python python-2.7 button tkinter


【解决方案1】:

替换以下行:

btnIco = Tkinter.Button(root, text="Icon", command=getFileName())

与:

btnIco = Tkinter.Button(root, text="Icon", command=getFileName)

换句话说,在getFileName 之后删除()。通过附加(),代码在创建按钮之前调用getFileName,并使用函数的返回值作为回调而不是函数本身。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2018-11-21
    • 2017-07-23
    • 2020-06-19
    相关资源
    最近更新 更多