I am an experience programmer, but relatively new to Python.
I have developed a fairly complex Python program on Raspberry Pi - in thonny, Raspbian/buster v10 - Python 3.7.3.
It uses tkinter, and works fine in thonny, but fails to create anything when the .py script is run from the command line.
I have reduced the program to a bare minimum, and it behaves the same:
:Run from thonny it works - I get the tkinter window
:Invoke python3 from CLI and enter commands individually - also works
But if I have it running in a script (test.py), the script runs fine (I get output) but no window appears.
import tkinter
root = tkinter.Tk() #produces small window in thonny & python3-line-by-line
#but nothing when the test.py script is run
print ('test") #to prove program is actually running
while 1: #loop to stop program terminating
l=3
No error messages.
Try to add root.mainloop() at the end of your code
The answer wasn't root.mainloop() exactly, because that would block any other code - but it pointed me in the right direction and I looked at root.update and root.update_idletasks .
I've pulled all the slide/canvas updates into a single function and added update/update_idletasks and it works in both thonny and via the command line.
Thanks for a very quick fix!
Tkinter applications need to be able to process a steady flow of events. Tkinter is single threaded (meaning: it can only do one thing at a time). By creating that infinite loop you've made it impossible for tkinter to service any events and thus the window will appear frozen (or not appear at all, since the drawing of the window is itself a response to an event).
You should remove your while loop and replace it with a call to root.mainloop().
Try using pycharm. I just fixed that problem. I realized that jupyter won't work
Had the same issue. I was trying to execute following command: $ python PATH_TO_PYTHON_SCRIPT. This always failed. The correct way to launch the script was $python3 PATH_TO_PYTHON_SCRIPT.
Related
I have started off with Python today and stuck with a weird problem. I am using Python 3.5.1 and Sublime text 3 and have written a basic hello world program.
print('Hello World')
Saved this file with the name python1.py and pressed Ctrl+B to execute it but the only thing that shows up on the console is [Finished in XYZs] which means that the program sucessfully executes but no output shown.
NOTE: I am using Windows 8(If that's important) and Python is installed properly and I can run my programs from CMD with no problems.
Solved it,
Press Ctrl+Shift+B after which a list pops up, select python from that list and its done. From next time just pressing Ctrl+B will work.
'Ctrl+Shift+B' Means "Build this code and then RUN it" instead of simply compile it
press Shift-Ctrl-B and select python. That happens because the program only checks if your python program is correctly. The next time just pressing Ctrl+B, will work
SHIFT-CTRL-B
CTRL-B ( next time onwards)
In my case I needed set Build System to Python.
Only it solved for me.
I've recently discovered Tkinter and its uses, but it only works when we run it from IDLE (file > open > F5)
I'd like to use it from the cmd window but it doesn't open the tkinter window and canevas
is there any way to open a python file from the cmd window as IDLE would have done it ?
IDLE executes a file nearly the same as python -i path/file.py -- notice the -i. The result is that tkinter windows are left on screen, and can be interacted with, after file.py execution stops. This is really handy for development. You likely have to add -i to your command line (if still developing), or add root.mainloop() (or the equivalent) at the end of your program. (There are parts of tkinter that do not work with .mainloop omitted, even in IDLE, but many part do.)
In 3.x, it is possible to write an import that works in IDLE and fails at the command line. But you did not mention getting an ImportError traceback.
If neither of these is your problem, then create and post a truly minimal working example, along with answers to Bryan's questions.
When using Ubuntu, I am able to play with Tk in real-time, by writing commands in the IDLE.
On Windows, I need to call root.mainloop() to spawn a window, and the loop keeps me from running any code from IDLE.
Is it a matter of platform, or am I doing something wrong?
I don't have a complete solution or answer, but here is something that I found on the subject:
From this page: Thinking in Tkinter
Note that you should not run these programs under IDLE. IDLE is itself a Tkinter application, with its own "mainloop" that will conflict with the mainloop in these programs. If you really want to view and run these programs using IDLE, then -- for each program -- you should comment out the "mainloop" statement in the program before running it.
I'm not too sure about the technical reasons behind it -- but I just don't run Tkinter code from IDLE.
I have written a Python module designed to solve the Tetris cube tiling puzzle, and it contains a lot of code, but it doesn't want to run, when I press f5 to run the program, the shell restarts, but then stops working, even when I enable debugging nothing happens still. I put a print statement at the very start of the program, yet it is not executed, and I cannot terminate the program using ctrl-c. I was able to fix this problem by putting some of the functions I had defined into a separate module, and now it works fine, but if I create a new function, and add enough print statements, the problem reappears, but can be fixed by just removing one statement.
The version of Python I am using is Python 2.7.8
The problem seems to have been an issue with the version of python I had installed. A new installation is able to run long modules fine, without any noticeable delay even from pressing f5 to first print statement.
When using Ubuntu, I am able to play with Tk in real-time, by writing commands in the IDLE.
On Windows, I need to call root.mainloop() to spawn a window, and the loop keeps me from running any code from IDLE.
Is it a matter of platform, or am I doing something wrong?
I don't have a complete solution or answer, but here is something that I found on the subject:
From this page: Thinking in Tkinter
Note that you should not run these programs under IDLE. IDLE is itself a Tkinter application, with its own "mainloop" that will conflict with the mainloop in these programs. If you really want to view and run these programs using IDLE, then -- for each program -- you should comment out the "mainloop" statement in the program before running it.
I'm not too sure about the technical reasons behind it -- but I just don't run Tkinter code from IDLE.