I am developing using Python Tools for Visual Studio in Visual Studio 2013 community edition on Windows 8.1. My problem is that I am unable to get a Tkinter window to start. I have tried using this code:
from tkinter import *
Tk()
When I launch this code from IDLE and such, I am able to get a tkinter window, as shown:
However, when I start this in Visual Studio, no Tkinter window appears, only the console window. No error is thrown. Example:
How do I get the Tkinter window to appear when I launch the program in Visual Studio with Python tools?
Edit: Also, when I try to do this from the Python interactive window in VS, this is what I get, with no window appearing:
>>> from tkinter import *
>>> Tk()
<tkinter.Tk object at 0x02D81FD0>
Most likely the problem is that you aren't starting the event loop. Without the event loop the program will exit immediately. Try altering your program to look like this:
import tkinter as tk
root = tk.Tk()
root.mainloop()
The reason you don't need to call mainloop in IDLE is because IDLE does that for you. In all other cases you must call mainloop.
Related
so I am trying to use Tkinter inside of VSCode on my M1 mac. When I use Tkinter through IDLE it works fine but once I try to open it inside of VSCode it the UI submenu just stalls. I'm using python 3.8.9 as my kernel but it won't seem to open even when I switch to any other versions. Tkinter is installed fine it is just some issue with how VSCode is running it but I have no idea how to fix it.
import tkinter as tk
window = tk.Tk()
Here I provided some screenshots of what happens
While using Idle
While using VSCode (The app just bounces up and down never opening the UI)
You have not started the event loop. IDLE does mainloop for you, otherwise, you need to call it by yourself.
import tkinter as tk
root = tk.Tk()
root.mainloop()
You can refer to here.
I'm trying to build simple window with buttons in Tkinter. Exit button should act with destroy() or sys.exit, but both option just lead to no response from window and only force close of Python helps. MacOs, Python 3.9
import tkinter as tk
from tkinter import ttk
import sys
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.attributes('-alpha',1)
self.attributes('-topmost',True)
self.overrideredirect(True)
self.resizable(False,False)
self.title('Monitor')
self.set_ui()
def set_ui(self):
exit_b=ttk.Button(self,text='Exit',command=self.app_exit)
exit_b.pack(fill=tk.X)
def app_exit(self):
self.destroy()
root=App()
root.mainloop()
Sorry for being noob, and not tested earlier, just switched to Mac from Windows. Simply running it for a test from IDE give this results, but when I export script as a python script with .py extension and running it straight with IDLE all works fine. There is definitely some problems with interaction between VS code IDE and Python interpreter for Apple silicon, cause it should not work like this, and its ridiculous to import script and execute it from IDLE every time just to test it as you write.
I can't get tkinter to work in Pycharm. There is no error message, but the window doesn't pop up. It works perfectly fine in IDLE. Even just a simple window doesn't work:
import tkinter as tk
window = tk.Tk()
window.mainloop()
The output looks like:
Process finished with exit code 0
I'm on mac os x with python 3.8 and pycharm 2020.1.2.
Some sources suggested installing the package future, but this doesn't work either.
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.
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.