python的tkinter按钮移动,Python Tkinter按钮动作

I have this code:

#!/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()

What I intended to do was to run the function getFileName when the button is clicked. But instead the function runs when the code is run and the button does not do anything when clicked. Can you please point out what is wrong?

解决方案

Replace the following line:

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

with:

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

In other word, remove () after getFileName. By appending (), the code is call getFileName before creating the button, and use the return value of the function as a callback instead of the function itself.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值