1

Code in question:

from tkinter import *

root = Tk()

test_label = Label(root, text = "Hello")
test_label.pack()

root.mainloop()

I can only run this python code from the IDLE, if I run it any other way the window flashes on the screen and closes immediately. I've tried:

-adding an "input" line to keep it from closing

-running from the windows powershell

-compiling the code into an EXE with pyinstaller

and now I can't find any other suggestions. I tried making a simple 1-line program that just asks for input, and that works normally and stays open fine. The tkinter program works fine in IDLE but not any other circumstance. What's happening here?

EDIT: If I run the program from the command line instead of windows 10 powershell, I get the following output:

Traceback (most recent call last):
  File "C:\Users\Cam\Desktop\CSCI Notes\Programs\test.py", line 1, in 
<module>
    import tkinter
ImportError: No module named tkinter

However, the tkinter file is in the python library on my computer, and importing tkinter in python shell or IDLE works fine.

Lawrence
  • 11
  • 1
  • 5
  • 2
    please don't link to code on another site. Please include a [mcve] in the question (eg: your "tiny little tkinter script") – Bryan Oakley Dec 15 '17 at 04:06
  • 1
    Thanks, I appreciate the etiquette tip. Fixed! – Lawrence Dec 15 '17 at 04:10
  • The usual way to debug problems like this is to run the program from a command prompt, so that you can see any error message that might be generated. You have apparently tried this already, so what exactly was the output when you ran it from a shell? – jasonharper Dec 15 '17 at 04:13
  • Have you tried to reinstall/update tkinter? – WCTech Dec 15 '17 at 04:15
  • When I ran from windows powershell, there was no error message or other output of any kind. The python shell flashed for less than a second and closed immediately, same as if I had double clicked on the .py file in windows explorer. – Lawrence Dec 15 '17 at 04:18
  • Your label seems to not have a parent. When I use your code, I get an empty window, but if I amend this `test_label = Label(root, text = "Hello")` I can see the label. – WCTech Dec 15 '17 at 04:23
  • You are correct! Unfortunately that did not solve my issue. I've made the change you suggested, and it hasn't changed the behavior. The program still works in IDLE, but nowhere else – Lawrence Dec 15 '17 at 04:26
  • I wouldn't recommend it, but you might be able to put everything into a `while True: ` loop – WCTech Dec 15 '17 at 04:32
  • I would test it in `cmd.exe`, not in powershell. – furas Dec 15 '17 at 04:36
  • In command prompt, If I type `import filename.py` in the python console after saving the file the window works for me – WCTech Dec 15 '17 at 04:46
  • Perhaps a long shot, but importing sys and adding the following line around the end worked for a similar problem in PyQt: sys.exit(app.exec_()) – Dlamini Dec 15 '17 at 04:48
  • @Dlamini `app.exec_()` runs PyQt's mainloop like `root.mainloop()` runs tkinter's mainloop – furas Dec 15 '17 at 08:35
  • @furas Many thanks, I learned something. – Dlamini Dec 15 '17 at 08:50

4 Answers4

0

When I run python on command prompt, it shows python 2.7, so I changed tkinter to Tkinter and the program worked by importing the .py file

Using cmd: enter image description here

Pyinstaller works(through canopy cmd prompt): enter image description here

the first line is what I put into my IDLE's cmd prompt

Which populates the folder when executed: enter image description here

And inside dist, I run test.exe which shows the window enter image description here

NOTE: my system command prompt is using python 2.7 while the canopy(python environment) command prompt uses 3.5 with pyinstaller

WCTech
  • 174
  • 2
  • 12
  • This is a known issue. Windows seem to usually default to python 2.7. This is though fixable see [this](https://stackoverflow.com/q/5087831/7032856). Also you could try adding `#! python3` as your very first line in the script to request a python3 interpreter. – Nae Dec 15 '17 at 08:17
0

Okay, I think I've solved this one! I read that windows can sometimes try to open .py files with python 2.7 instead, even if they're written in 3.6. I uninstalled python 2.7 from my computer, and now the file runs normally.

So for anybody having this problem, try to make sure your computer is opening python 3, not python 2.

Lawrence
  • 11
  • 1
  • 5
0

I have also had this problem and as far as I can see no one has a proper solution.

The best I have found is putting -i before your filename. For example: python -i yourfile.py

this will start the IDLE in the command line

MJCS
  • 31
  • 1
  • 6
0

This could also happen if you are using a modulo that isn't installed in your computer even if you have installed it using your IDLE. Try installing it with the command "pip install modulo" else try installing it manually.

arya
  • 1