Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help understand loops
#1
I have 3 loops that i am confused about.
tkinter.mainloop()
Is this loop specifically for tkinter module and it will run independent from the rest of the code ?
mainloop()
is this main loop that runs independent of any modules that are currently running ?
while 1:
    code here
What kind of loop is that then ?

How to exit or terminate tkinter.mainloop() or mainloop(), with a break ? Where in the code would i place it ?
Thanks !
Reply
#2
Can you share the code of mainloop() function, without knowing the code, we can't conclude the actual functionality/behavior of the loop

while 1:
    code here
This will create infinite loop, programmatically we should avoid such loops unless it has purpose
Reply
#3
Is mainloop a function built into tkinter ? Because it seems python does not understand it by its self unless it says tkinter.mainloop()

Purpose of the loop is to display GUI.

I am trying to understand how to run 2 independent loops. In other words, i want multitasking.
I cant really paste any code because i dont understand how loops work in python.

while 1:
    print(1)
while 1:
    print(2)
This is the basic i have. I only 1 printed because only one loop runs at the time.
How do make 2 loops to run at same time ?


Thansk !
Reply
#4
(Nov-27-2019, 04:43 AM)tonycstech Wrote: Is this loop specifically for tkinter module and it will run independent from the rest of the code ?
All GUI or web-app(server) has some kind of main loop that run continuous.
If think of it so are these application something that you don't want to stop,before you push eg a stop button.

tonycstech Wrote:is this main loop that runs independent of any modules that are currently running ?
No,if you make loop or something that interfere with the main loop GUI is running it will freeze GUI.
Here a example from doc,i have put in time.sleep this will freeze GUI main loop for 10-sec.
A normal way is to have a quit button as here that break GUI main loop.
import tkinter as tk
import time

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        time.sleep(10)
        print("hi there, everyone!")        

root = tk.Tk()
app = Application(master=root)
app.mainloop()
Usually all GUI's has some kind of methods build in for Threading(Multithreading).
A very common mistake that many makes with GUI is to freeze the main loop not thinking of this.
Tkinter has eg after.
Example here dos it print hello world every 10-sec,but it dos not freeze GUI as i showed with time.sleep.
import tkinter as tk

class Foo(tk.Tk):
    def __init__(self):
        super().__init__()
        self.callId = None
        self.button = tk.Button(self, text="Stop", command=self.stop)
        self.button.pack()

    def periodically_speak(self):
        print("Hello world!")
        self.callId = self.after(10000, self.periodically_speak)

    def stop(self):
        if self.callId is not None:
            self.after_cancel(self.callId)

foo = Foo()
foo.periodically_speak()
Reply
#5
You can also check https://python-forum.io/Thread-Tkinter-H...ng-the-gui
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
The keyword i was looking for is multi threading, thank you.
foo lol. what the hell is a foo anyway.
Foo vs foo, very confusing.
If Foo is a class name. why not just call it a ClassName to make it more obvious ?
Is it like a jargon of programmers ?
if things were named little more obvious, i'd have less questions to ask.
Its like i have to understand black English if i enter the hood for what ever reason. Just makes it harder.

Can you please explain why only one loop running in the example below ?
I expected both at same time.

from threading import Thread

def Loop1():
    while 1:
        print(1)
def Loop2():
    while 1:
        print(2)

process1 = Thread(target=Loop1)
process2 = Thread(target=Loop2)
process1.run()
process2.run()
Thank you.
Reply
#7
(Nov-27-2019, 06:51 AM)tonycstech Wrote: Is it like a jargon of programmers ?
https://stackoverflow.com/a/6727104/4046632
https://en.wikipedia.org/wiki/Foobar

And because we are on python forum and python has it's own jargon - very often instead of foo and bar you will see spam and eggs instead - i.e. yet another reference to Monty Python

(Nov-27-2019, 06:51 AM)tonycstech Wrote: if things were named little more obvious, i'd have less questions to ask.
To be honest one of the qualities that a programmer should poses is to be able to research unfamiliar areas on their own. No matter is it a new language, new package (i.e. reading/understanding the documentation), domain specific non-programming matters, etc. Of course there are always questions you may need little/extra help, but most of the cases you will be able to find an naswer and even if not sufficient, the effort you put in doing the research would help understand better the help you get.
Most of your questions can be answered with a little research via google or via search tool on the forum.
(Nov-27-2019, 07:10 AM)tonycstech Wrote: Foo vs foo
As per PEP8 naming convention Foo is a class name (i.e. starts with CAPITAL F. foo is variable (all lower case)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Got it. Thank you.
Didnt know Monty Python show was popular in Netherlands.
In any event, can i get some help with loop confusion i am having ?
I need to understand how printing 1 and 2 can be done at same time and not one after another.
Reply
#9
(Nov-27-2019, 08:43 AM)tonycstech Wrote: I need to understand how printing 1 and 2 can be done at same time and not one after another.
You most use start() to spawn separate thread.
run() does not spawn a separate thread,it runs the thread function in the context of the current thread.
from threading import Thread
import time

def Loop1():
    while 1:
        time.sleep(2)
        print(1)

def Loop2():
    while 1:
        time.sleep(6)
        print(2)

process1 = Thread(target=Loop1)
process2 = Thread(target=Loop2)
process1.start()
process2.start()
Reply
#10
from tkinter import messagebox
from threading import Thread

def Loop1():
    while 1:
        messagebox.showinfo("Title", "1")
def Loop2():
    while 1:
        messagebox.showinfo("Title", "2")

process1 = Thread(target=Loop1)
process2 = Thread(target=Loop2)
process1.start()
process2.start()
I should be getting 2 messages popped but i only see one constantly.
Second message does not trigger because its stuck in a loop 1

I understand that loop 1 prevents loop 2 to start until its broken but isnt what Threading was for ?
Why i dont get two message boxes at same time ?
It seems to work with print but not with message box.
Thank you.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020