Tkinter opens console window with window - python

I am a complete newbie at Python. Meanwhile, I'm working with Tkinter to integrate a GUI into my application using Python 2.7. This is the code so far:
import Tkinter
top = Tkinter.Tk()
top.mainloop()
However, when I execute the file, a console window and the GUI pops up. How do I get rid of the console window during startup?

Rename your Python script as .pyw (not .pyc). This will tell the invoker to to instantiate a console window. Source
Note however, this will work for non-GUI based scripts too which can cause undesireqable behaviour - such as not being able to see your script.

Related

Why does my TKinter GUI code work from interactive shell, but not when run from a file?

I am trying to take some of my codes that have a command line interface and give them GUI's. However, I am running into a problem. Can anyone help me understand why when I run my code from a file, nothing happens, but if I run it interactively, it works fine? A simple example is below. BTW, I am running Python 3.8.1 on Windows 10. Thanks in advance!
import tkinter as tk
import tkinter.ttk as ttk
#--------------------------
window = tk.Tk()
window.title('Test Window')
window.geometry('1000x800')
This is because you don't call the mainloop function. Tkinter automatically processes events when run interactively, but you need to explicitly start the event loop when not running interactively. Your code is running, but because you never tell it to start listening for events it exits at the end of the file just like any other python script.
You should add window.mainloop() as the last line in your file.

Terminate Python Shell (IDLE) programmatically

I am using IDLE (Python 3.6 64-bit). Currently, I am using Tkinter to create a GUI. I have a button in my code that is supposed to terminate the program. As of now, it only closes out the "tk" root window (using root.destroy()). I want it to also close out the shell and go back to where I can edit the code (i.e. back to my IDLE). How can I do this?
When you run code from an IDLE editor window, any existing program run from IDLE is terminated. Unless your code somehow grabs control of the mouse, you should always be able to click on any editor window or its taskbar icon (on Windows) and start editing some more. Example: run the following from an IDLE editor.
import tkinter as tk
root = tk.Tk()
#root.destroy()
A tk window is dispayed. Click on the editor window, remove the '#', and run again. The first tk window disappears and a >>> prompt appears in the Shell window.
A python program run from IDLE, cannot* close its parent IDLE, anymore than a python program run in a console can close the parent console.
I am excluding things like, on *nix, getting the parent process number and issuing a 'kill ' command.

Hide console window of python .py on windows

I found this answers on Stackoverflow but nothing works for me
How to hide console window in python?
Hiding the console window
Hide console window with Tkinter and cx_Freeze
I'm trying to hide console window, on windows at least. But with solutions above still I have window open
The only difference maybe I have to mark, I run python application with process from C# application, but as I understand it can't be a reason of some key different if I write all directives from the python code. Now I'm not sure how to figure out
You need to use the pythonw.exe program as it won't open a terminal when ran.

How do I get a tkinter window to launch when running a program in PyCharm?

I recently started using PyCharm rather than IDLE for the programming course I am doing, with the promise of being able to run programs without constantly having to save them and then go to the menu to type "run in module" to do so, and to also have multiple files open in tabs to easily swap between.
While this has been good for the later, and I finally fixed a directory problem for the former, the current project I am working on involves tkinter, and PyCharm refuses to launch a tkinter window when the file is run. It can print statements fine and do calculations, but it will not launch a tkinter window. The program works fine in IDLE, but I'd rather not do all my editing in PyCharm only to constantly have to open and run it in IDLE to make the bloody window pop up. I'm using python 3.5.
Does anyone have a solution?
from tkinter import*
tk = Tk()
btn = Button(tk, text="Click Me")
btn.pack()
tk.mainloop()
The tk.mainloop() [or root.mainloop() if your Tk()=root] at the end or your code makes a little window pop up.
Hope this helps.

PyGTK Window Crashes When Created

I am trying to create a simple PyGTK window running on Windows 7. I used the PyGTK-all-in-one installer from: ftp.gnome.org to install the necessary PyGTK dependencies for 32-Bit Python 2.7.2. However, whenever I create a simple GTK window in IDLE using the code:
import gtk
mainWindow = gtk.Window()
mainWindow.show()
the window is displayed, but instantly stops responding and is unable to be moved, re-sized, etc. Is there a simple way to prevent this from happening?
Thank you in advance!
Running my code from the PyGtk Command Prompt rather than IDLE prevents the window from crashing.
Edit: Adding a main loop to my code, as noted here, prevents the window from crashing when run with IDLE.

Categories

Resources