How to 'pause' Tkinter mainloop()? [duplicate] - python

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.

Related

Is there a way to be able to use the python shell to call functions while the tkinter window is running

I wrote a program to start and stop an AutoHotKey script in the tkinter GUI for my non-coding coworkers to use but the autohotkey script has to be able to send commands through the python shell and I just realized that while the tkinter window is open, I am unable to send commands through the shell. I know theres an AutoHotKey library for Python but I really don't want to have to rewrite too much if possible. Is there anyway to keep the tkinter window up and running while also calling other functions from the shell?
I don't know what to try other than rewriting the whole thing using the AHK library.

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.

tkinter not working when run from command line python script

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.

Tkinter and execute a python file as in IDLE

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.

mainloop prevents Tkinter application to run from the IDLE

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.

Categories

Resources