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.
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 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.
The goal is for the user to choose a file and then the program to process it:
from tkinter import filedialog, Tk
root = Tk()
root.withdraw()
filename = filedialog.askopenfilename()
root.destroy()
# Long running process after this...
This works fine in windows, but in mac it leaves a Python app not responding in the dock bar after root.destroy().
The program still runs in the background and process the file, but I wonder if this "Not responding" app in the dock can be removed or hidden after root.destroy().
My setup:
Python 2.7.17 and 3.7.5.
macOS Catalina 10.15.4
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.
Using python 3.6 on macOS 10.12 I cannot get any function out of tkinter.
It imports just fine but then calling Tk() or filedialog() immediately causes this error Window of:
Python quit unexpectedly
I updated Tcl/Tk from ActiveState and this appeared to have no effect.
I have used tkinter once for a class but I don't have a lot of experience with it, am I simply calling the wrong functions? It seems like any function from the tkinter library causes the same problem.
from tkinter import *
Tk()
OR
filedialog.askopenfilename()