Well, I've written a simple program that gives you info about your computer. The program runs perfectly fine, but the console window pops up and really does nothing. So I would like to hide the console window and only show the gui frame. Pointers on how to do this?

Recommended Answers

All 12 Replies

You have two options.

1. Save the file as a .pyc instead of a .py.
2. Try "root.withdraw()". (replace root with whatever you named your root widget)

Or you could compile it with py2exe if you want to get fancy and Windows only. (if you choose this option, let me know, there's an extra step)

Don't want to hijack this but I'm interested in compiling a tkinter gui app with py2exe -- the catch -- I'm using python 3.1.1 or whatever the latest version is.

I only see py2exe up until 2.6/7?

I used cxfreeze, and it gave me a .exe file... however when I open the .exe file it just displays a console window for about 1/10 of a second and then closes. I think it has something to do with tkinter because when I used cxfreeze on a plain old user input, then print user input program it worked fine.

Should I just make my code in 2.6 or is there a better solution for converting a tkinter gui app into a .exe?

Don't want to hijack this but I'm interested in compiling a tkinter gui app with py2exe -- the catch -- I'm using python 3.1.1 or whatever the latest version is.

I only see py2exe up until 2.6/7?

I used cxfreeze, and it gave me a .exe file... however when I open the .exe file it just displays a console window for about 1/10 of a second and then closes. I think it has something to do with tkinter because when I used cxfreeze on a plain old user input, then print user input program it worked fine.

Should I just make my code in 2.6 or is there a better solution for converting a tkinter gui app into a .exe?

I don't think Tkinter is your problem, the fact that you are using Python 3.x is the problem. As far as I know, py2exe is not compatible with Python 3.0x. I would probably just convert all my code to 2.6/7 for the time being if I were you.

I did not use py2exe, I used cxfreeze. If tkinter is not the problem then why did my gui program in exe form fail and my console one work? Not saying that the Python version isn't the case or that tkinter is definitely the problem, just throwing out questions. I'll work on converting it to 2.6.

You should run your program with pythonw.
This can be done in your command line or by renaming your .py file .pyw

I saved it as a .pyw, there was no console as wanted. Whenever I convert it to a .exe through cxfreeze it still doesn't work though.

Here is the code.

from tkinter import * 

print ('Ignore the console, it is here only for bug reports.') 

root = Tk() 

root.geometry("300x150+300+300") 


pilotpln= 0 
enempln = 0 


class AppUI(Frame): 

    def __init__(self, master=None):
        Frame.__init__(self, master, relief=SUNKEN, bd=2)

        self.menubar = Menu(self) 

        menu = Menu(self.menubar, tearoff=0) 
        self.menubar.add_cascade(label="File", menu=menu) 
        menu.add_command(label="Save", command=writefile) 
        menu.add_command(label="Exit", command=root.quit)

        try:
            self.master.config(menu=self.menubar)
        except AttributeError:
            
            self.master.tk.call(master, "config", "-menu", self.menubar)


def setnumpilot(event): 
    global pilotpln
    if var.get() == 'MOSQ':
        pilotpln = 57
    elif var.get() == 'P-38J':
        pilotpln = 82
    elif var.get() == '109F-4':
        pilotpln = 13


def setnumenem(event):
    global enempln
    if var2.get() == 'MOSQ':
        enempln = 57
    elif var2.get() == 'P-38J':
        enempln = 82
    elif var2.get() == '109F-4':
        enempln = 13
        
name = pilotpln 


#str0 = filename1 I only have the strs that equal pilot/enempln here for my reading, they don't have to be.

str1 = \
"""
0x0
ndisles
(default)
0
[MISSION]
One On One P51Ds East
0
0
0:0:0:0:0.00:0.00:0.00:0.00:0.00
0,0
0,0
0
[ROUTES]
P51D Solo Route
[WAYPOINTS]
78799.12,0.00,-68238.32,78909.61,0.00,-68340.31,0
[SEGMENTS]
0,0,14.67,10.25,0.00,150.37,0.00,0.00,0.00
[ENDSEGMENTS]
78909.61,0.00,-68340.31,82594.45,0.00,-72018.68,1
[SEGMENTS]
0,1,234.67,22.19,500.00,5206.57,0.00,0.00,0.00
[ENDSEGMENTS]
82594.45,0.00,-72018.68,85624.53,0.00,-75121.86,0
[SEGMENTS]
1073741824,3,366.67,11.83,500.00,4337.18,0.00,0.00,0.00
[ENDSEGMENTS]
85624.53,0.00,-75121.86,24662.35,0.00,-134308.29,0
[SEGMENTS]
1375731712,3,366.67,231.73,14000.00,84967.18,0.00,0.00,0.00
[ENDSEGMENTS]
24662.35,0.00,-134308.29,-29689.92,0.00,-134308.29,0
[SEGMENTS]
1073741824,5,440.00,123.53,14000.00,54352.27,0.00,0.00,0.00
[ENDSEGMENTS]
-29689.92,0.00,-134308.29,-30687.21,0.00,-83446.53,0
[SEGMENTS]
1073741824,5,440.00,115.62,14000.00,50871.54,0.00,0.00,0.00
[ENDSEGMENTS]
-30687.21,0.00,-83446.53,23714.99,0.00,-83874.91,0
[SEGMENTS]
1073741824,5,440.00,123.65,14000.00,54403.88,0.00,0.00,0.00
[ENDSEGMENTS]
23714.99,0.00,-83874.91,86398.53,0.00,-57225.37,0
[SEGMENTS]
1375731716,3,293.33,232.20,1500.00,68113.32,0.00,0.00,0.00
[ENDSEGMENTS]
86398.53,0.00,-57225.37,72585.98,0.00,-71411.75,-2147483648
[SEGMENTS]
1073741824,8,293.33,67.50,1000.00,19800.00,0.00,0.00,0.00
[ENDSEGMENTS]
72585.98,0.00,-71411.75,83204.67,0.00,-81750.64,-2147483648
[SEGMENTS]
1073741824,9,293.33,50.52,750.00,14820.57,0.00,0.00,0.00
[ENDSEGMENTS]
83204.67,0.00,-81750.64,87808.86,0.00,-77021.84,-2147483648
[SEGMENTS]
1073741824,10,293.33,22.50,500.00,6600.00,0.00,0.00,0.00
[ENDSEGMENTS]
87808.86,0.00,-77021.84,80973.19,0.00,-70366.31,1
[SEGMENTS]
1073741824,11,234.67,40.66,0.00,9540.57,0.00,0.00,0.00
[ENDSEGMENTS]
[ENDWAYPOINTS]
[FLIGHTS]
East To West
196608,10,1,"""

str2 = str(pilotpln)
str21 = ','
str22 = str(pilotpln)

str3 = \
""",0.000000,0.000000,0.000000,78774.522643,0.000000,-68214.061304,0,0.000000
[WEAPONSETS]
0
0
0
1
[ENDWEAPONSETS]
[ENDFLIGHTS]
[ENDROUTES]
[GROUPNAME]
"""
#str0
str30 = \
"""
0x0
[ENDGROUPNAME]
[ENDMISSION]
[MISSION]
One On One P51Ds West
1
1
0:0:0:0:0.00:0.00:0.00:0.00:0.00
0,0
0,0
0
[ROUTES]
P51D Solo
[WAYPOINTS]
-83376.87,0.00,-30590.44,-83490.17,0.00,-30692.41,0
[SEGMENTS]
0,0,14.67,10.39,0.00,152.42,0.00,0.00,0.00
[ENDSEGMENTS]
-83490.17,0.00,-30692.41,-88135.27,0.00,-35326.18,0
[SEGMENTS]
0,1,234.67,27.96,500.00,6561.16,0.00,0.00,0.00
[ENDSEGMENTS]
-88135.27,0.00,-35326.18,-90050.32,0.00,-37547.21,0
[SEGMENTS]
1073741824,3,366.67,8.00,500.00,2932.64,0.00,0.00,0.00
[ENDSEGMENTS]
-90050.32,0.00,-37547.21,-9756.81,0.00,-134696.10,0
[SEGMENTS]
1375731712,3,366.67,343.73,14000.00,126035.53,0.00,0.00,0.00
[ENDSEGMENTS]
-9756.81,0.00,-134696.10,45883.98,0.00,-134025.73,0
[SEGMENTS]
1073741824,5,440.00,126.47,14000.00,55644.83,0.00,0.00,0.00
[ENDSEGMENTS]
45883.98,0.00,-134025.73,46554.35,0.00,-84418.27,0
[SEGMENTS]
1073741824,5,440.00,112.75,14000.00,49611.99,0.00,0.00,0.00
[ENDSEGMENTS]
46554.35,0.00,-84418.27,-13199.90,0.00,-84900.41,0
[SEGMENTS]
1073741824,5,440.00,135.81,14000.00,59756.19,0.00,0.00,0.00
[ENDSEGMENTS]
-13199.90,0.00,-84900.41,-77573.40,0.00,-20025.08,0
[SEGMENTS]
1375731713,3,293.33,311.57,1500.00,91393.42,0.00,0.00,0.00
[ENDSEGMENTS]
-77573.40,0.00,-20025.08,-91414.89,0.00,-34183.23,-2147483648
[SEGMENTS]
1073741824,8,293.33,67.50,1000.00,19800.00,0.00,0.00,0.00
[ENDSEGMENTS]
-91414.89,0.00,-34183.23,-80817.32,0.00,-44543.77,-2147483648
[SEGMENTS]
1073741824,9,293.33,50.52,750.00,14820.57,0.00,0.00,0.00
[ENDSEGMENTS]
-80817.32,0.00,-44543.77,-76203.49,0.00,-39824.39,-2147483648
[SEGMENTS]
1073741824,10,293.33,22.50,500.00,6600.00,0.00,0.00,0.00
[ENDSEGMENTS]
-76203.49,0.00,-39824.39,-83025.55,0.00,-33154.91,1
[SEGMENTS]
1073741824,11,234.67,40.66,0.00,9540.57,0.00,0.00,0.00
[ENDSEGMENTS]
-83025.55,0.00,-33154.91,-85497.40,0.00,-30685.70,0
[SEGMENTS]
1073741824,12,88.00,0.00,0.00,0.00,0.00,0.00,0.00
[ENDSEGMENTS]
-85497.40,0.00,-30685.70,-85634.34,0.00,-30540.21,0
[SEGMENTS]
1073741824,0,14.67,13.62,0.00,199.81,0.00,0.00,0.00
[ENDSEGMENTS]
-85634.34,0.00,-30540.21,-85274.87,0.00,-30197.86,1
[SEGMENTS]
1073741824,0,14.67,33.85,0.00,496.40,0.00,0.00,0.00
[ENDSEGMENTS]
[ENDWAYPOINTS]
[FLIGHTS]
West To East
196608,10,1,10"""

str33 = str(enempln) + ','
str34 = ','
str4 = str(enempln)

str5 = \
""",0.000000,0.000000,0.000000,-83370.343819,0.000000,-30572.162808,0,0.000000
[WEAPONSETS]
0
0
0
1
[ENDWEAPONSETS]
[ENDFLIGHTS]
[ENDROUTES]
[GROUPNAME]
"""
#str6 = filename1

str7 = \
"""
0x0
[ENDGROUPNAME]
[ENDMISSION]
"""



def writefile():
    x = var.get() #x is the name of the plane the user selects for himself
    space = " "
    y = 'vs'
    space2 = " "
    z = var2.get() #name of plane for the enemy
    zz = '.mis' #has to be a .mis file
    filename = x + space+ y + space2 + z + zz  #put it all together and whaddya get?  X vs Y.mis!
    filename1 = x + space+ y + space2 + z
    str6 = filename1
    str0 = filename1
    str2 = str(pilotpln) #had to define within function.  I used them above with the other strings for readability
    str4 = str(enempln) 
    str21 = ','
    str22 = str(pilotpln)
    str33 = str(enempln)
    str34 = ','
    
    done = 1 
    namelist = [str0,str1,str2,str21,str22,str3,str0,str30,str33,str34,str4,str5,str0,str7]
    while done: 
        if type(name) == type(""):
            namelist.append(name)
            done = 0
        else:
            done = 0

        FILE = open(filename,"w") 
        FILE.writelines(namelist) 

        FILE.close() 


app = AppUI(root) 
app.pack() 

var = StringVar()
var.set("Choose a plane for yourself.") 

var2 = StringVar()
var2.set("Choose a plane for the enemy.")

OptionMenu(root, var, 'P-51D',"P-38J","109F-4", command=setnumpilot).pack() 
OptionMenu(root, var2, 'P-51D',"P-38J","109F-4", command=setnumenem).pack()

b = Button(root, text="Save mission file", fg="blue", command=writefile)
b.pack()

quit = Button(root, text="QUIT", fg="red", command=root.quit)
quit.pack()


root.mainloop()

All programs of mine that use tkinter do not work when convereted to .exe with cxfreeze. Doesn't matter if I convert with setup.py build or the normal way. Also doesn't matter if they were .py or .pyw.

Instead of hi-jacking a thread, please make a new one. But are there still no answers for my problem.

Instead of hi-jacking a thread, please make a new one. But are there still no answers for my problem.

Sorry, I had a brain fart. My first post said you should change the file to a .pyc (WRONG), you should make it a ".pyw" as jice said.

To do this, just save the file as a .pyw file instead of a .py from whatever text editor you are using and it should work.

Alternatively, you could use the "withdraw()" method of your root window. Example:

import Tkinter
root = Tk()
root.withdraw()

Your question was answered, sorry for the hijack though. I had my mind on other things.

Looks like I need to mark this thread.

If you are using cx_freeze to freeze your python app, then you need to use a distutils script as follows, just save this as setup.py (changing the name of the script you want to freeze), then run as python setup.py build, and you will have an app without the console window.

from cx_Freeze import setup, Executable


setup(
        name = "hello",
        version = "0.1",
        description = "the typical 'Hello, world!' script",
        executables = [Executable("myScript.py", base = "Win32GUI" )])
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.