Author Topic: Defined function running before it is called  (Read 5174 times)

0 Members and 1 Guest are viewing this topic.

Offline Spyro543

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1189
  • Rating: +74/-3
    • View Profile
Defined function running before it is called
« on: August 19, 2011, 05:09:04 pm »
This program is nowhere near finished, just warning you!

I'm working on a GUI for g++ because I'm bored. I'm having strange problems with it.

Code: (entire source) [Select]
from Tkinter import *
import os
import tkFileDialog

root = Tk()

tn = Label(root, text="gcc C++ Compiler GUI")
tn.grid(row=0, column=1, columnspan=3, sticky=N+S+E+W)

pathn = Label(root, text="Path to source:")
pathn.grid(row=1, column=1, sticky=N+S+E+W)

namen = Label(root, text="Name of compiled file:")
namen.grid(row=2, column=1, sticky=N+S+E+W)

path = Entry(root)
path.grid(row=1, column=2, columnspan=2, sticky=N+S+E+W)
path.delete(0, END)
path.insert(0, "")

name = Entry(root)
name.grid(row=2, column=2, columnspan=3, sticky=N+S+E+W)
name.delete(0, END)
name.insert(0, "")

quitbtn = Button(root, text="Exit", fg="red", command=exit)
quitbtn.grid(row=3, column=3, sticky=N+S+E+W)

compilebtn = Button(root, text="Compile!", fg="blue", command=quit)
compilebtn.grid(row=3, column=2, sticky=N+S+E+W)

spyro = Label(root, text="By Spyro543")
spyro.grid(row=3, column=1, sticky=N+S+E+W)

def fopen():
    cformats = [('C++ source file','*.cpp'),('Plain text file','*.txt'),('Any File','*.*')]
    filenm = tkFileDialog.askopenfile(parent=root,filetypes=cformats ,title='Select the cpp source file')
    path.insert(0, filenm)

browsebtn = Button(root, text="Browse", fg="black", command=fopen())
browsebtn.grid(row=1, column=4, sticky=N+S+E+W)

root.mainloop()

I'm having two problems: one is the definde function fopen() is running immediately instead of when I call it (when the button is pressed). Any way to fix this?
Code: (first problem) [Select]
def fopen():
    cformats = [('C++ source file','*.cpp'),('Plain text file','*.txt'),('Any File','*.*')]
    filenm = tkFileDialog.askopenfile(parent=root,filetypes=cformats ,title='Select the cpp source file')
    path.insert(0, filenm)

My 2nd problem is that after fopen() runs (when it's not supposed to :mad:) all of the widgets freeze and don't do anything. I can't activate the text boxes and none of the buttons do anything.
« Last Edit: August 21, 2011, 08:09:26 am by Spyro543 »

Offline Spyro543

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1189
  • Rating: +74/-3
    • View Profile
Re: Defined function running before it is called
« Reply #1 on: August 21, 2011, 08:09:54 am »
Bump. Still needing help. >:P

Offline bsl

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 157
  • Rating: +14/-0
    • View Profile
Re: Defined function running before it is called
« Reply #2 on: August 21, 2011, 11:36:16 am »
This is a typical bug when Tkinter programming.
You use lambda functions.
Change the line from:
Code: [Select]
browsebtn = Button(root, text="Browse", fg="black", command=fopen())

to
Code: [Select]
browsebtn = Button(root, text="Browse", fg="black", command=lambda:fopen())
« Last Edit: August 21, 2011, 11:36:58 am by bsl »

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Defined function running before it is called
« Reply #3 on: August 21, 2011, 12:02:15 pm »
Wouldn't it also just work to change it to browsebtn = Button(root, text="Browse", fg="black", command=fopen) ?
You're passing a function, not its result.
Edit: In other words, by using fopen(), you are calling the function.
« Last Edit: August 21, 2011, 12:03:33 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline bsl

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 157
  • Rating: +14/-0
    • View Profile
Re: Defined function running before it is called
« Reply #4 on: August 21, 2011, 12:30:43 pm »
Yes, that works for void functions.
The advantage with lambda is that it allows you to pass arguments
should Spyro543 decide later to do so on fopen:

browsebtn = Button(root, text="Browse", fg="black", command=(lambda: fopen(arg1,arg2,...)))

Offline Spyro543

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1189
  • Rating: +74/-3
    • View Profile
Re: Defined function running before it is called
« Reply #5 on: August 21, 2011, 02:08:51 pm »
So if fopen() has arguments, I place a "lambda: " infront of it? And if it doesn't, I don't put the () after fopen?

EDIT: One more problem: I get <open file u'C:/Users/Aaron/Downloads/stuff.cpp', mode 'r' at 0x01F85EE8> instead of C:/Users/Aaron/Downloads/stuff.cpp.
« Last Edit: August 21, 2011, 02:12:03 pm by Spyro543 »

Offline bsl

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 157
  • Rating: +14/-0
    • View Profile
Re: Defined function running before it is called
« Reply #6 on: August 21, 2011, 03:03:06 pm »
Change the line in sub fopen to read:

path.insert(0, filenm.name)

instead of:

path.insert(0, filenm)