I had a question...Im using TKinter in python trying to build a GUI to run some command prompt scripts others have made. I am having some trouble though...pretty early on...

I can open a file and get the file name, but I can not even pass the file name to another function. I'm sure this is an easy fix I have been struggling with it for a day and can't figure it out. Thanks.

So I have created 2 buttons,

Class App:
 
   def __init__(self, masterb)
      frame = Frame(masterb)
      frame.pack()
 
      Button(frame, text="open", command=self.askopenfilename).pack(side=LEFT)
      
      Button(frame, text="execute", command=self.printdef).pack(side=LEFT)
 
root.mainloop()
 
 
def askopenfilename(self)
   filename = tkFileDialog.askopenfilename(**self.options)
 
   print filename
   return filename
 
 
#so far this works fine...the problem I have is taking the filename string and using it in another function.  I feel like this is very easy and basic to do I just cant do it.
 
def printdef()
   
   words = askopenfilename(filename)    #I've also tried words = askopenfilename()
   print words
 
root = Tk()
 
app = App(root)

Just wanted to mention, my problem is I can not print words

I get an exception in tkinter call back

Traceback (most recent call last):
File "C:\Pythin24\lib\lib-tk\Tkinter.py, in __call__
return self.func(*agrs)
TypeError: printdef() takes no arguments (1 given)

I had a question...Im using TKinter in python trying to build a GUI to run some command prompt scripts others have made. I am having some trouble though...pretty early on...

I can open a file and get the file name, but I can not even pass the file name to another function. I'm sure this is an easy fix I have been struggling with it for a day and can't figure it out. Thanks.

So I have created 2 buttons,

Class App:
 
   def __init__(self, masterb)
      frame = Frame(masterb)
      frame.pack()
 
      Button(frame, text="open", command=self.askopenfilename).pack(side=LEFT)
      
      Button(frame, text="execute", command=self.printdef).pack(side=LEFT)
 
root.mainloop()
 
 
def askopenfilename(self)
   filename = tkFileDialog.askopenfilename(**self.options)
 
   print filename
   return filename
 
 
#so far this works fine...the problem I have is taking the filename string and using it in another function.  I feel like this is very easy and basic to do I just cant do it.
 
def printdef()
   
   words = askopenfilename(filename)    #I've also tried words = askopenfilename()
   print words
 
root = Tk()
 
app = App(root)

That code is fundamentally wrong, I do not understand your post. It is impossible to run that code.

For example you have defs without ':'

IDLE 2.6.4      
>>> def askopenfilename(self)
   filename = tkFileDialog.askopenfilename(**self.options)
 
   print filename
   return filename
 

SyntaxError: invalid syntax
>>> Class App:
	
SyntaxError: invalid syntax
>>>

You are trying to use modules without import , then you define function with same name yourself ETC.

The actual code is on another network so I am unable to copy and paste it over...I just transcribed it quickly by hand...the code runs on my machine and I get the error I mentioned above.

That code is fundamentally wrong, I do not understand your post. It is impossible to run that code.

For example you have defs without ':'

IDLE 2.6.4      
>>> def askopenfilename(self)
   filename = tkFileDialog.askopenfilename(**self.options)
 
   print filename
   return filename
 

SyntaxError: invalid syntax
>>> Class App:
	
SyntaxError: invalid syntax
>>>

You are trying to use modules without import , then you define function with same name yourself ETC.

def without colon can not run!

To see real object oriented Tk code see this quote from vegaseat earlier thread and see the code behind the link :

TCL code that makes up Tkinter is very forgiving and can lead to very poor programming styles.

If you want to use a class approach to Tkinter, I recommend a look at:
http://www.daniweb.com/forums/post11...ml#post1146095

There root is simply self itself.

As I mentioned the actual code resides on another computer not hooked up to internet...I mis transcibed the code quickly in hopes of getting a fast responce...I apologize, here is what the code really looks like.

Class App:
 
   def __init__(self, masterb):

      frame = Frame(masterb)
      frame.pack()
 
      Button(frame, text="open",command=self.askopenfilename).pack(side=LEFT)
      
      Button(frame, text="execute", command=self.printdef).pack(side=LEFT)

      self.options = options ={}
      options['filetypes'] = [('all files'. '.*'), ('text files', '.txt')
      options['initialdir'] = 'C:\\'

root.mainloop()
 
 
def askopenfilename(self):
   filename = tkFileDialog.askopenfilename(**self.options)
 
   print filename
   return filename
 
def printdef():
   
   words = self.askopenfilename()
   print words
 
root = Tk()
 
app = App(root)

The error is also differant now...since I updated words=sel.askopenfilename()

When I click the execute button it now prompts me to open the file as well. I want the open button to select my file, then the execute button to just print words. Thanks.

Just wanted to mention, my problem is I can not print words

I get an exception in tkinter call back

Traceback (most recent call last):
File "C:\Pythin24\lib\lib-tk\Tkinter.py, in __call__
return self.func(*agrs)
TypeError: printdef() takes no arguments (1 given)

The indention is still wrong. Everything should be inside the class and the self parameter is missing from the printdef.

What do you by the way accomplish by printing GUI selected files name one or two times to console depending on button pressed. Why those button names that have nothing to do with the function?

Please assume the code is written proply on the other network.

In anycase I figured out the problem. I needed to attatch filename to the App class in the init function. Then instead of messing with the words variable I can just call self.filename.

Sorry about the malformed code, its a big pain to rewrite it twice on the diff network...next time i post I will be more careful. I thought that people could get past the poor format and assume a base knowledge and help me with my problem...guess not. NExt time I will be percise.

The indention is still wrong. Everything should be inside the class and the self parameter is missing from the printdef.

What do you by the way accomplish by printing GUI selected files name one or two times to console depending on button pressed. Why those button names that have nothing to do with the function?

Next time give your pseudo code for the logic to criticize, not so error prone.

Good that you found the same thing I was about to suggest yourself. That way is always better than others telling you.

Funny, thing, but object oriented programming goes little backwards from functional programming. Current state is best kept in objects variables, instead of using parameters and return values. Functions can also be put in variables, lists etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.