Tkinter askopenfilename在脚本开始

时间:2017-07-05 09:51:21

标签: python tkinter tk

我在python中遇到askopenfilename模块的小问题。 我为catch文件创建了一个小GUI,在运行另一个脚本之前选择并添加选项。

目前我已经定义了我的GUI,我想添加用于调用askopenfilename的按钮。

但我的问题是当我运行我的脚本时,askopenfilename直接启动而不是单击我的特定按钮。

我与你分享我的问题代码。

感谢您的帮助。

##Import##
import os
import sys
import math
import pickle
import shutil
import paramiko
import Tkinter, tkFileDialog
import Tix


from Tkinter import *
def open():
    tkFileDialog.askopenfilename(initialdir="U:/calcul/projects/", title="job file selector",parent=fen1)
###Graphical interface##    
#principal window
fen1=Tk()
fen1.title("utilitaire de lancement serveur")
v=IntVar()
tex=StringVar()
tex.set('abc \n timmy')
#slave widget
txt1=Label(fen1, text='Fichier Dat').grid(row=0, sticky=W)
entr1=Text(fen1, width=50,height=2).grid(row=1, sticky=W)
b1=Button(fen1, text='select file', command=open()).grid(row=2,sticky=E)
text2=Label(fen1, text='number of core').grid(row=3, sticky=W)
check1=Radiobutton(fen1, text="1 core", variable=v, value=1).grid(row=4, column=0)
check2=Radiobutton(fen1, text="8 core", variable=v, value=8).grid(row=5, column=0)
check3=Radiobutton(fen1, text="12 core", variable=v, value=12).grid(row=6, column=0)
text3=Label(fen1, text='Dependencies').grid(row=7, sticky=W)
entr2=Entry(fen1).grid(row=8, column=0)
text4=Label(fen1, text='log').grid(row=9, sticky=W)
mes1=Message(fen1,textvariable=tex,bg='white',relief='sunken',bd=3).grid(row=10, column=0)
b2=Button(fen1, text='quitter',command= fen1.quit).grid(row=11,column=1)
fen1.mainloop()
print v.get()

1 个答案:

答案 0 :(得分:0)

这一行:

b1=Button(fen1, text='select file', command=open()).grid(row=2,sticky=E)

是问题所在。按下按钮调用功能时需要跳过括号:

b1=Button(fen1, text='select file', command=open).grid(row=2,sticky=E)

你实际做的是在脚本开始时调用你的函数一次,然后它使用函数返回的函数(None)作为要调用的命令,显然当你随后按下按钮这样就不能调用它什么也没做。您上面所拥有的完整(和工作)版本:

##Import##
import os
import sys
import math
import pickle
import shutil
import paramiko
import Tkinter, tkFileDialog
import Tix


from Tkinter import *
def open():
    tkFileDialog.askopenfilename(initialdir="U:/calcul/projects/", title="job file selector",parent=fen1)
###Graphical interface##    
#principal window
fen1=Tk()
fen1.title("utilitaire de lancement serveur")
v=IntVar()
tex=StringVar()
tex.set('abc \n timmy')
#slave widget
txt1=Label(fen1, text='Fichier Dat').grid(row=0, sticky=W)
entr1=Text(fen1, width=50,height=2).grid(row=1, sticky=W)
b1=Button(fen1, text='select file', command=open).grid(row=2,sticky=E) # note that i removed the brackets after "command=open"
text2=Label(fen1, text='number of core').grid(row=3, sticky=W)
check1=Radiobutton(fen1, text="1 core", variable=v, value=1).grid(row=4, column=0)
check2=Radiobutton(fen1, text="8 core", variable=v, value=8).grid(row=5, column=0)
check3=Radiobutton(fen1, text="12 core", variable=v, value=12).grid(row=6, column=0)
text3=Label(fen1, text='Dependencies').grid(row=7, sticky=W)
entr2=Entry(fen1).grid(row=8, column=0)
text4=Label(fen1, text='log').grid(row=9, sticky=W)
mes1=Message(fen1,textvariable=tex,bg='white',relief='sunken',bd=3).grid(row=10, column=0)
b2=Button(fen1, text='quitter',command= fen1.quit).grid(row=11,column=1)
fen1.mainloop()
print v.get()

还值得注意的是,有一个名为open的内置函数通过调用您的函数同样的事情,对原始(打开文件)的任何调用都不起作用。应该避免调用与内置函数相同的函数。