0

I want to run a program that after being load will refresh tkinter canvas automatically.

I want the other process to be independent from tkinter main loop

In pseudo code i want it to behave something like this:

if __name__ == "__main__":
    app = loadTkinterWindowsWithCanvas()
    app.mainloop()
    manager = Manager.Manager()
    dataProcess = Process(target = manager.start())
    dataProcess.start()
    oldX = None
    x = None
    while True:
        time.sleep(1)
        oldX, x = x , manager.x
        if oldX is not x:
            app.updateCanvas(x) 
...
#in Manager Class
    def start(self):
        self.x = 0    
        while True:
            self.x += 1

I was trying out a lot of examples from stackoverflow or tutorials, with process threads,lock and event but i cant get behavior i desire. My program either doesn't load or doesn't update. What is best approach ?

This is what my code looks right now

if __name__ == "__main__":
    lock = mp.RLock()
    app = MainFrame()
    listOfCities = [(0,121),(112,5),(14,201),(45,88),(141,231),(1,8),(22,11),(101,84),(90,231)]
    pop = Population.Population(200,listOfCities)
    manager = EvolutionManager.EvolutionManager(100,pop,lock)
    pro = mp.Process(target = manager.startTraining())
    pro.start()
    app.mainloop()
    lastBest = None
    best = None
    while True:
        print("x")
        time.sleep(0.2)
        lastBest, best = best,manager.population.bestSalesman
        if lastBest is not best:
            app.getCurrentTopFrame().updateFrame(best.dna.getAsListOfTuple())

Behavior is that while second process start window does not load until the process ends

UPDATE

This started to work.

def genethicAlgorithmPart(event,manager):
    manager.startTraining(event)
def addChangerListiner(manager,app,event):
    thread = t.Thread(target = changeListiner, args = (manager,app,event,))
    thread.start()
def changeListiner(manager,app,event):
    lastBest = None
    best = None
    print("Starting listiner thread")
    while True:
        print("x")
        event.wait()
        lastBest, best = best,manager.population.bestSalesman
        if lastBest is not best:
            app.getCurrentTopFrame().updateFrame(best.dna.getAsListOfTuple())
        event.clear()
if __name__ == "__main__":
#     listOfCities = [(631, 44), (612, 137), (441, 266), (447, 173), (52, 243), (104, 148), (333, 70), (474, 182), (419, 221), (238, 291), (264, 340), (290, 213), (332, 97), (473, 294), (188, 198), (180, 258), (433, 382), (394, 139)]
    listOfCities = rmg.genRandomListOfPoints(15,800,400)
    pop = Population.Population(400,listOfCities)
    manager = EvolutionManager.EvolutionManager(100,pop)
    event = mp.Event()
    pro = t.Thread(target = genethicAlgorithmPart, args = (event, manager,))
    app = MainFrame.MainFrame()
    app.getCurrentTopFrame().updateFrame(manager.population.bestSalesman.dna.getAsListOfTuple())
    app.after(111, addChangerListiner(manager, app, event))
    pro.start()
    app.mainloop()
Adrian
  • 149
  • 3
  • 16
  • 1
    Please refer to this [answer](https://stackoverflow.com/a/29158947/5319738), Your code will not go to your `while` loop because of the call to `mainloop` – Kamal Dec 03 '18 at 03:18
  • Thanks!, yes, that was one issue, i have uploaded code that works for me right now. – Adrian Dec 03 '18 at 22:31

0 Answers0