Execute Tkinter from "compiled" Python script

Post here if you need help with creating a Graphical User Interface in Python.

Execute Tkinter from "compiled" Python script

Postby supporter » Mon Dec 23, 2013 10:59 pm

I'm new to python and am learning how to write GUI-oriented code using examples that are available on many websites.

I'm using WINXP, Python 2.7 with IDLEX 1.11.2 and Notepad++.

This "self-teaching" goes reasonbly well, but I experience one anoying difficulty: my test programs with TKINTER function well when executed by clicking "run" in IDLEX, but when I execute a "compiled" program (a .pyc file) directly by clicking the file, then I see a black screen during a fraction of a second, and because of this "flash" I don't have the time to see the "window" with buttons that should be displayed by my program. Adding "raw_input" statements do not remedy the problem. When executing the ".pyc" file, I cannot click any button because the system does not seem to wait for user input, nor do I have the time to see the outputs of the "print" and the "raw_input" statements.

I attached my Python code.

Could someone be so kind to tell me what I need to do (which statements I need to add?) to avoid that the window "flashes" over my screen without allowing me to click any button?
Last edited by Yoriz on Mon Dec 23, 2013 11:22 pm, edited 1 time in total.
Reason: First post lock
supporter
 
Posts: 5
Joined: Mon Dec 23, 2013 10:21 pm

Re: Execute Tkinter from "compiled" Python script

Postby Yoriz » Mon Dec 23, 2013 11:28 pm

Hi, welcome to the forum.
Please read the 'new users, read this' link in my signature.

There is no code attached but it would be better if you were to add the code in code tags as decsribed in the above link.

When you change to using GUI frameworks you no longer need to use print and raw_input, use the gui itself for input and output.
Dont use the .pyc just use the .py.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 837
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Execute Tkinter from "compiled" Python script

Postby supporter » Tue Dec 24, 2013 12:04 am

My way of "attaching the program code) clearly did not function.

Therefore I followed Yoriz' recommendations and added the source code of my test program below.



Code: Select all
"""
First display a window with a couple of buttons in it.

    If "Destroy Window" button is clicked, then remove the displayed window from the screen, display a msg "Window removed",
     and after and "enter" stop processing
    If Exit" button is clicked, the exit the program.
"""

try:
    import Tkinter as tk ## Tkinter in Python 2.x
except ImportError:
    import tkinter as tk ## tkinter in Python 3.x
   
class DestroyTest():

    def __init__(self, top):
        self.top=top
        self.top.geometry("+10+10")
        self.frame=tk.Frame(self.top)
        self.frame.grid()
        test_label=tk.Label(self.frame, text="Label")
        test_label.grid(row=1, column=0)
        destroy_button=tk.Button(self.frame, text="Destroy Window", command=self.destroy)
        destroy_button.grid(row=10, column=0)
        exit_button=tk.Button(self.top, text="Exit", command=top.quit)
        exit_button.grid(row=10, column=0)
    def destroy(self):
        self.top.destroy() #Destroy the window
        mydata = raw_input("Window removed")

print ("1")
mydata = raw_input("just wait for enter key")
root=tk.Tk()
DT=DestroyTest(root)
root.mainloop()
raw_input()
supporter
 
Posts: 5
Joined: Mon Dec 23, 2013 10:21 pm

Re: Execute Tkinter from "compiled" Python script

Postby metulburr » Tue Dec 24, 2013 12:16 am

Try running the code from command line/terminal instead of IDLE, or from another IDE completely. I am not familiar with IDLEX. Googling it just looks like extesnions for IDLE. IDLE is a bad IDE for tkinter as the mainloops interfere with each other, because IDLE is written in python/tkinter. IDLE and your program are both trying to use the mainloop.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Execute Tkinter from "compiled" Python script

Postby supporter » Tue Dec 24, 2013 12:27 am

Hello Metulburr,

Thx for your recommendation.
Yes, IDLEX is based on IDLE. Of course I will change to another IDE if there are better ones.
Can you recommend a simple-to-use one that does not cost money and that, in your opinion, is better?
Thx in advance.
supporter
 
Posts: 5
Joined: Mon Dec 23, 2013 10:21 pm

Re: Execute Tkinter from "compiled" Python script

Postby metulburr » Tue Dec 24, 2013 1:26 pm

Can you recommend a simple-to-use one that does not cost money and that, in your opinion, is better?

I think every IDE is better than IDLE. Everyone here i believe considers IDLE the worst IDE of them all. I would never pay for an IDE. There are too many free ones that are just as good as the ones that costs.

A list of python IDE's
https://wiki.python.org/moin/Integrated ... vironments

there is also sublime text 2. This is free to use, but you get a message requesting you to buy it every so often.
http://www.sublimetext.com/2

Personally, i use Geany when coding, and use Vim/terminal for server scripts.

So what is the result of running the code under a terminal/command line or from another IDE?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Execute Tkinter from "compiled" Python script

Postby supporter » Tue Dec 24, 2013 2:01 pm

Hello metulburr,

Thx for the list. I'll try a few soon.

But..., I have the impression that the problem is not with the IDE because the problem only shows up when I run the prog directly under Windows (in the PVM). W.o.w. I "compiled" the program and created a .pyc, and then I clicked the filename of the "compiled" program (= the .pyc file), and then the "flashing" happens. When I run the prog in source code under an IDE (even when the IDE is IDLEX) it runs fine, I see the Window with the bullets appearing (and disappearing after I click the button)! This could indicate that the problem is not with the IDE, because under the IDE all is fine. However it does not work correctly when executing it directly in the PVM.

Has anybody here an idea what the reason (and solution) could be?
supporter
 
Posts: 5
Joined: Mon Dec 23, 2013 10:21 pm

Re: Execute Tkinter from "compiled" Python script

Postby metulburr » Tue Dec 24, 2013 2:07 pm

Yoriz wrote:Dont use the .pyc just use the .py.

+1
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Execute Tkinter from "compiled" Python script

Postby supporter » Mon Dec 30, 2013 8:47 pm

When I ran the script as a "command" in de command processor of Windows, the "flashing" occured.
When I ran de script by double clicking the .py file, the flashing occured.
When I ran the "compiled" script (the .pyc file) by double clicking it, the flashing occured.
When I did "run" the script in IDLEX, it worked fine.
When I ran the scripit as a "build: in Sublime, I got an error because Sublime cannot handle user input

Meanwhile, I uninstalled Python (2.7.5) and installed 2,7,6 and related packages (IDLEX and Sublime). Now I can run the script as a "command:, I can doulbe-click the .py file, and I can double click the .pyc file. In all these cases it works fine. In IDLEX "run" all works fine. When executing the script in Sublime "build", then it shows that Subime still cannot handle user input, it terminates in error.

Of the two "IDE's" that I have installed, Sublime makes better use of colors to differentiatie between variables, commands, etc..., but the "build" cannot handle user input in "raw_input". IDLEX works fine, including its "run" function, but the displayed program is more difficult to read.

Looks as if my problem is "closed", although it still is a mistery why 2.7.5 behaved so strange!
supporter
 
Posts: 5
Joined: Mon Dec 23, 2013 10:21 pm


Return to GUI

Who is online

Users browsing this forum: Bing [Bot] and 2 guests