[Tutor] Executing a GUI from another GUI with a click of a button

Alan Gauld alan.gauld at btinternet.com
Tue Apr 7 00:38:14 CEST 2009

"Chris Lee" <i-scream_cones at hotmail.com> wrote

> Before I tell you about my problems, I ask for your consideration
> as I am a newbie at python...

That's OK, its what the tutor list is for! :-)

> I was having problems as I tried to open another program
> within a program (both programs are Tkinter UIs)

OK, That can be tricky, especially if you want the two programs
to communicate but it is possible.

> def showtime():
>     os.system("clock.py")
> window = Tk()
> frame = Frame(window)
> frame.pack()

> clock_command = lambda : showtime()
> b3 = Button(frame, text="What time is it?", command=clock_command)

Why not miss out the lambda and just do:

b3 = Button(frame, text="What time is it?", command=showtime)

lambdas are there to save you creating names for small one-liner type 
In you case the onle liner just calls another function to you might as well 
use the function directly as your command.

> b3.pack()
> window.mainloop()

> And the following code is the clock.py:
> ------------------------
#program: clock.py
import Tkinter
import time

currenttime = ''
clock = Tkinter.Label()

def tick():
    global currenttime
    newtime = time.strftime('%H:%M:%S')
    if newtime != currenttime:
        currenttime = newtime
    clock.after(200, tick)

> -----------------------

> I know for certain that the "clock.py" works (because i got it from the 
> web.. -_-;;)

And you ran it from a command prompt and it worked ok?

Its a very unusual Tkinter program in that it doesn't have any of
the usual Tkinter preamble...

> but I'm having a real difficulty trying to open clock.py through my real 
> program....

That might be bbecause you are calling clock.py directly instead of
invoking the interpreter. That may depend on the Operating system
you are using however...

Can you tell us what does happen?

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list