Python quits whenever tkinter is called - python

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()

Related

Python 3.8.9 MacOS M1 tkinter UI not opening inside of VSCode

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.

Python freezes upon Tkinter destroy() method

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.

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.

Running Python file from command line does not load modules

I have a Python script that contains the following modules:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
When I run the code in IDLE by pressing F5 the script runs fine and starts my app.
However, when I go to the command prompt and type
python ScannerApp.py
I get the following error:
File "tkinterTest.py", line 1, in <module>
from tkinter import *
ImportError: No module named tkinter
How do I get rid of this error? The ultimate goal being to make this script into a .exe.
One thought is that python is not added to my environmental variables under Path, it is added as it's own variable. Could that be causing the issue?
My question does not pertain to the difference between Tkinter and tkinter. My question was about why when I ran code through the command line I was getting an error. The issue happened to be that my environmental variable python was set to run python 2.7 instead of the necessary python 3.6 (which uses tkinter).
Try adding this for cross compatibility instead of your previous import code.: (Hoping that's the problem)
try:
from tkinter import *
from tkinter import ttk,filedialog
except:
from Tkinter import *
from Tkinter import ttk,filedialog
The solution to my problem was to change the environmental variable python to run version 3.6 instead of 2.7.
The issue was a cross compatibility issue and I found it easier to change the variable instead of trying to have it try both Tkinter and tkinter modules depending on the particular version.
Your issue might be that python3 doesn't use Tkinter (with a capital T) but tkinter. That is if you're using pyhton3 of course ^^
https://stackoverflow.com/a/17843652/9368855

Tkinter opens console window with window

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.

Categories

Resources