How to stop other processes or finish processes in Python / Tkinter program -
i have been drawing graphics python / tkinter program. programme has main menu menu items drawing different figures. works quite came against problem. if programme part way through drawing 1 figure , user clicks draw sec figure programme draws sec figure, when has finished drawing sec figure goes , finishes drawing first figure. want stop drawing first figure , not go drawing first figure when sec figure has finished drawing. created simpler illustration programme demonstrate scenario. see problem in programme click "draw -> red" , click "draw -> blue" before reddish has finished drawing. how programme abort previous drawing? here illustration program:
tkinter import * import random import math def line(canvas, w, h, p, i): x0 = random.randrange(0, w) y0 = random.randrange(0, h) x1 = random.randrange(0, w) y1 = random.randrange(0, h) canvas.create_line(x0, y0, x1, y1, fill=p.col(i)) class color: def __init__(self, r, g, b): self.red = r self.gre = g self.blu = b def hexval(self, v): homecoming (hex(v)[2:]).zfill(2) def str(self): homecoming "#" + self.hexval(self.red) + self.hexval(self.gre) + self.hexval(self.blu) class palette: def __init__(self, n0, y): self.colors = [] self.n = n0 self.m = 0 if y == "red": self.red() elif y == "blue": self.blue() def add(self, c): self.colors.append(c) self.m += 1 def red(self): self.add(color(127, 0, 0)) self.add(color(255, 127, 0)) def blue(self): self.add(color(0, 0, 127)) self.add(color(0, 127, 255)) def col(self, i): k = % (self.n*self.m) z = k // self.n j = k % self.n c0 = self.colors[z] c1 = self.colors[(z + 1) % self.m] t0 = (self.n - j)/self.n t1 = j/self.n r = int(math.floor(c0.red*t0 + c1.red*t1)) g = int(math.floor(c0.gre*t0 + c1.gre*t1)) b = int(math.floor(c0.blu*t0 + c1.blu*t1)) c = color(r, g, b) homecoming c.str() def upd(canvas): try: canvas.update() homecoming true except tclerror: homecoming false def tryline(canvas, w, h, p, i, d): try: line(canvas, w, h, p, i) if % d == 0: upd(canvas) homecoming true except tclerror: homecoming false class menuframe(frame): def __init__(self, parent): frame.__init__(self, parent) self.parent = parent self.initui() def initui(self): self.width = 800 self.height = 800 self.canvas = canvas(self.parent, width=self.width, height=self.height) self.pack(side=bottom) self.canvas.pack(side=top, fill=both, expand=1) self.parent.title("line test") menubar = menu(self.parent) self.parent.config(menu=menubar) self.parent.protocol('wm_delete_window', self.onexit) menu = menu(menubar) menu.add_command(label="red", command=self.onred) menu.add_command(label="blue", command=self.onblue) menu.add_command(label="exit", command=self.onexit) menubar.add_cascade(label="draw", menu=menu) self.pred = palette(256, "red") self.pblue = palette(256, "blue") def onred(self): # how abort here processes running? self.canvas.delete("all") in range(0, 7000): tryline(self.canvas, self.width, self.height, self.pred, i, 100) upd(self.canvas) def onblue(self): # how abort here processes running? self.canvas.delete("all") in range(0, 7000): tryline(self.canvas, self.width, self.height, self.pblue, i, 100) upd(self.canvas) def onexit(self): self.canvas.delete("all") self.parent.destroy() def main(): root = tk() frame = menuframe(root) root.mainloop() if __name__ == '__main__': main()
that's simple example? ;)
you can add together variable tracks selected method, check if variable exists before completing for
loop. here's simpler example:
class example(frame): def __init__(self, parent): frame.__init__(self, parent) button(self, text='task a', command=self._a).pack() button(self, text='task b', command=self._b).pack() self.current_task = none # var hold current task (red, blue, etc) def _a(self): self.current_task = 'a' # set current task in range(1000): if self.current_task == 'a': # go on loop if current task print('a') self.update() def _b(self): self.current_task = 'b' in range(1000): if self.current_task == 'b': print('b') self.update() root = tk() example(root).pack() root.mainloop()
let me know if doesn't create sense or doesn't work out you.
python tkinter
No comments:
Post a Comment