0

I want to create two times in one tkinter window using multiprocessing. I want to run two timers simultaneously. Here is the code. First it shows the window with the given text and then it shows error.

Code:

from multiprocessing import Process
from time import time, sleep
from tkinter import *

class Main:
    def __init__(self):
        self.root = Tk()
        self.root.geometry('300x300')

        self.lab1 = Label(self.root, text='None')
        self.lab1.place(x=10, y=10)

        self.lab2 = Label(self.root, text='None')
        self.lab2.place(x=10, y=50)

        self.root.mainloop()

    def c1(self):
        st = time()
        for _ in range(10):
            self.lab1['text'] = int(time()-st)
            self.root.update()
            sleep(1)

    def c2(self):
        st = time()
        for _ in range(10):
            self.lab2['text'] = int(time()-st)
            self.root.update()
            sleep(1)

    def main(self):

        self.p1 = Process(target=self.c1)
        self.p2 = Process(target=self.c2)

        self.p1.start()
        self.p2.start()

        self.p1.join()
        self.p2.join()


if __name__ == '__main__':
    start = Main()
    start.main()
  • Each `Process` is an entirely separate environment, with no access to the objects in the original process. If they had a GUI at all, they would each have their own separate window. But there's no need to go to such lengths to have a timer, even multiple timers, in Tkinter - use the `.after()` method to schedule the next update of each timer. – jasonharper Aug 07 '20 at 14:52
  • Why the requirement for multiprocessing? That makes the problem a lot more complicated. – Bryan Oakley Aug 07 '20 at 15:47
  • Here's some documentation on the universal widget [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) method. There are many questions with answer here show how to use it to implement timers (if you look). – martineau Aug 07 '20 at 15:48
  • Look on this answer here: https://stackoverflow.com/questions/63118430/create-a-main-loop-with-tkinter/63118515#63118515 – Thingamabobs Aug 07 '20 at 17:04
  • Thanks for the answers. Got the solution with ```.after()```. – Sarabjeet Sandhu Aug 07 '20 at 17:41

0 Answers0