2

I'm trying to make a launcher for my Python program with Tkinter. I used the execfile function, and fortunately it opened the target GUI. However, none of the buttons would work, and it would say the global variable most functions reference isn't defined.

The code to launch the program:

def launch():
    execfile("gui.py")

That works. The base code for the target program:

from Tkinter import *
gui = Tk()
gui.title("This is a GUI")

EDIT: Example of a button:

def buttonWin():
    buttonWindow = Toplevel(gui)
    button = Button(buttonWindow, text = "Button", width = 10, command = None)
    button.pack()

When it references that 'gui' variable for Toplevel, it comes up with an error. I tried defining the 'gui' variable in the Launcher script, but that only caused the target script to open first, instead of the Launcher:

gui = Tk()
launcher = Tk()
launcher.title("Launcher")
def launch():
    return execfile("gui.py")
launchButton = Button(launcher, text = "Launch", width = 10, command = launch)

When I try pressing one of this program's buttons, I get a NameError: $NameError: Global variable 'gui' is not defined$ Also this is in Python 2.7.5. Thank you anyone who answers, and sorry for any errors with the code blocks; I'm new.

Majora64
  • 23
  • 4
  • There are no buttons in that code fragment, and what's there isn't properly formatted, so it's a bit hard to know what to make of your question. Does the script work if you run it with the Python interpreter from the command line? Can you strip down your example to something that still fails but is small enough to post? – Fredrik Jul 25 '13 at 17:32
  • Okay, I edited the post to include the Button code. And actually, my computer for some reason doesn't let me run programs that I haven't stored in my Python27 folder. When I type "python launcher.py" in the command line, it just says "'python' is not a recognized application or batch file." And sorry that it's not clear, I have slight problems in communicating these things. – Majora64 Jul 27 '13 at 08:55

1 Answers1

1

The problem is that you have structured the Tkinter program incorrectly.

In "gui.py" you should have something like:

from Tkinter import *

gui= Tk()
gui.mainloop()

You can add buttons to perform functions and customize it:

from Tkinter import *

gui = Tk()
gui.title("This is a GUI")    

def launch():
    execfile("gui.py")

launchbutton = Button(gui, text='Launch Program', command=launch)
launchbutton.pack()

gui.mainloop()

I think with your function buttonWin you were trying to do what is normally handled by a class; see unutbu's answer here.

I'm not sure if I've addressed your problem, but this should be a start.

Community
  • 1
  • 1
Al.Sal
  • 984
  • 8
  • 19
  • Thanks, I actually have just that, though the launcher object assigned to Tk() is called 'launcher' instead of 'gui'. But when I do that, it does launch the target application on command, but none of the functions/buttons work on it. – Majora64 Aug 03 '13 at 00:50
  • @Majora64 So launcher.py and gui.py are separate modules? – Al.Sal Aug 05 '13 at 12:39
  • Yes, yes they are. Also sorry for my late response. – Majora64 Aug 10 '13 at 21:26
  • Hm. `import` might suit your purposes better. – Al.Sal Aug 19 '13 at 13:54