-1

I encounter small issue with askopenfilename module in python. I create a small GUI for catch file, select and add option before running another script.

Presently i have define my GUI and i want to add button for call the askopenfilename.

But my problem is when i run my script askopenfilename start directly and not on click on my specific button.

I share with you my problematic code.

thanks for your help.

##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()
  • I think it's not duplicate because the solution provide in question attached do not solve my problem or maybe i do not understand clearly this solution. – quentin Jul 05 '17 at 12:24
  • 1
    That linked question has the exact solution. Check your `b1=Button(fen1, text='select file', command=open()).grid(row=2,sticky=E)` line. – Lafexlos Jul 05 '17 at 13:13

1 Answers1

0

this line:

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

is the problem. to call a function with a button when pressed you need to skip the brackets:

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

what you were actually doing is calling your function once at the start of the script, then it uses what the function returns (None) as the command to be called, obviously it cannot be called when you subsequently press the button so it does nothing. a complete (and working) version of what you have above:

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

also worth noting that there is a built in function called open by calling your function the same thing any calls to the original (to open a file) will not work. calling functions the same as built in functions should be avoided.

James Kent
  • 5,763
  • 26
  • 50