python - Resizing causes errors? -


i'm learning, slowly! part of code below question on here resizing objects in tkinter canvas. anyway aim make starting grid lights, learn more add ability of starting , stopping buttons or keys, want working visually. want fit perfect screen run on without changing code etc, when re size window starts adding circles in different places , getting me confused.

import tkinter import time  class redraw():     def __init__(self):         self._tk = tkinter.tk()          self._myframe = tkinter.frame(master = self._tk)         self._myframe.grid(row = 0, column = 0, sticky = tkinter.nsew)          self._mycanvas = tkinter.canvas(master = self._tk, width = 850, height = 400, bg = 'grey', highlightthickness = 0)         self._mycanvas.grid(row = 0, column = 0, sticky = tkinter.nsew)           self._mycanvas.bind("<configure>", self._on_resize)          self._tk.rowconfigure(0, weight = 1)         self._tk.columnconfigure(0, weight = 1)          self.fracx = 1         self.fracy = 1          self._oval1 = self._mycanvas.create_oval(50*self.fracx,50*self.fracy,160*self.fracx,160*self.fracy, fill="black", tags = 'oval1')         self._oval2 = self._mycanvas.create_oval(210*self.fracx,50*self.fracy,320*self.fracx,160*self.fracy, fill="black", tags = 'oval2')         self._oval3 = self._mycanvas.create_oval(370*self.fracx,50*self.fracy,480*self.fracx,160*self.fracy, fill="black", tags = 'oval3')         self._oval4 = self._mycanvas.create_oval(530*self.fracx,50*self.fracy,640*self.fracx,160*self.fracy, fill="black", tags = 'oval4')         self._oval5 = self._mycanvas.create_oval(690*self.fracx,50*self.fracy,800*self.fracx,160*self.fracy, fill="black", tags = 'oval5')      def _on_resize(self, event):         self._mycanvas.delete(tkinter.all)         self.fracx = event.width/850         self.fracy = event.height/400         self._oval1 = self._mycanvas.create_oval(50*self.fracx,50*self.fracy,160*self.fracx,160*self.fracy, fill="black", tags = 'oval1')         self._oval2 = self._mycanvas.create_oval(210*self.fracx,50*self.fracy,320*self.fracx,160*self.fracy, fill="black", tags = 'oval2')         self._oval3 = self._mycanvas.create_oval(370*self.fracx,50*self.fracy,480*self.fracx,160*self.fracy, fill="black", tags = 'oval3')         self._oval4 = self._mycanvas.create_oval(530*self.fracx,50*self.fracy,640*self.fracx,160*self.fracy, fill="black", tags = 'oval4')         self._oval5 = self._mycanvas.create_oval(690*self.fracx,50*self.fracy,800*self.fracx,160*self.fracy, fill="black", tags = 'oval5')  def main():     canvas = redraw()     # widgets canvas     def red0(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._tk.update()             time.sleep(0.05)                 def red1(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._mycanvas.delete('oval2')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval2')              canvas._tk.update()             time.sleep(0.05)                     def red2(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._mycanvas.delete('oval2')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval2')              canvas._mycanvas.delete('oval3')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval3')              canvas._tk.update()             time.sleep(0.05)                      def red3(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._mycanvas.delete('oval2')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval2')              canvas._mycanvas.delete('oval3')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval3')              canvas._mycanvas.delete('oval4')             canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval4')              canvas._tk.update()             time.sleep(0.05)                     def red4(a):         in range(a):             canvas._mycanvas.delete('oval5')             canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval5')              canvas._tk.update()             time.sleep(0.05)          def green(a):         in range(a):             canvas._mycanvas.delete(tkinter.all)             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval1')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval2')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval3')             canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval4')             canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval5')             canvas._tk.update()             time.sleep(0.05)     def amber(a):         in range(a):             canvas._mycanvas.delete(tkinter.all)             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="yellow", tags = 'oval1')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="yellow", tags = 'oval2')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="yellow", tags = 'oval3')             canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="yellow", tags = 'oval4')             canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="yellow", tags = 'oval5')             canvas._tk.update()             time.sleep(0.05)     def lights1(a):         in range(a):             canvas._mycanvas.delete(tkinter.all)             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval1')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval2')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval3')             canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval4')             canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval5')             canvas._tk.update()             time.sleep(0.05)     def lights():         canvas._mycanvas.delete(tkinter.all)         canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval1')         canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval2')         canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval3')         canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval4')         canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval5')         canvas._tk.update()      # tag of drawn widgets     def flashing():         global flag         flag=1         while flag:             if flag == 1:                  lights1(9)                 amber(10)                 lights()     def stop():         global flag         flag = 2         while flag:             if flag == 2:                 lights()      def start():         global flag         flag = 3         while flag:             if flag == 3:                 lights()                 red0(30)                 red1(30)                 red2(30)                 red3(30)                 red4(30)                 green(30)                 lights()                 break       canvas._but1 = tkinter.button(text="start", command=start)     canvas._but1.grid(row = 2, column = 0, sticky="w")     canvas._but2 = tkinter.button(text="amber", command=flashing)     canvas._but2.grid(row = 2, sticky="w", padx=(80))     canvas._but1 = tkinter.button(text="stop", command=stop)     canvas._but1.grid(row = 2, column = 0, sticky="w", padx=(40))  if __name__ == "__main__":     main() 

re re edited sorted of problems still think it's rather messy still getting loads of errors

import tkinter import time  class redraw():     def __init__(self):         self._tk = tkinter.tk()          self._myframe = tkinter.frame(master = self._tk)         self._myframe.grid(row = 0, column = 0, sticky = tkinter.nsew)          self._mycanvas = tkinter.canvas(master = self._tk, width = 850, height = 400, bg = 'grey', highlightthickness = 0)         self._mycanvas.grid(row = 0, column = 0, sticky = tkinter.nsew)           self._mycanvas.bind("<configure>", self._on_resize)          self._tk.rowconfigure(0, weight = 1)         self._tk.columnconfigure(0, weight = 1)          self.fracx = 1         self.fracy = 1          self._oval1 = self._mycanvas.create_oval(50*self.fracx,50*self.fracy,160*self.fracx,160*self.fracy, fill="black", tags = 'oval1')         self._oval2 = self._mycanvas.create_oval(210*self.fracx,50*self.fracy,320*self.fracx,160*self.fracy, fill="black", tags = 'oval2')         self._oval3 = self._mycanvas.create_oval(370*self.fracx,50*self.fracy,480*self.fracx,160*self.fracy, fill="black", tags = 'oval3')         self._oval4 = self._mycanvas.create_oval(530*self.fracx,50*self.fracy,640*self.fracx,160*self.fracy, fill="black", tags = 'oval4')         self._oval5 = self._mycanvas.create_oval(690*self.fracx,50*self.fracy,800*self.fracx,160*self.fracy, fill="black", tags = 'oval5')      def _on_resize(self, event):         self._mycanvas.delete(tkinter.all)         self.fracx = event.width/850         self.fracy = event.height/400         self._oval1 = self._mycanvas.create_oval(50*self.fracx,50*self.fracy,160*self.fracx,160*self.fracy, fill="black", tags = 'oval1')         self._oval2 = self._mycanvas.create_oval(210*self.fracx,50*self.fracy,320*self.fracx,160*self.fracy, fill="black", tags = 'oval2')         self._oval3 = self._mycanvas.create_oval(370*self.fracx,50*self.fracy,480*self.fracx,160*self.fracy, fill="black", tags = 'oval3')         self._oval4 = self._mycanvas.create_oval(530*self.fracx,50*self.fracy,640*self.fracx,160*self.fracy, fill="black", tags = 'oval4')         self._oval5 = self._mycanvas.create_oval(690*self.fracx,50*self.fracy,800*self.fracx,160*self.fracy, fill="black", tags = 'oval5')  def main():     canvas = redraw()     # widgets canvas     def red0(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._tk.update()             time.sleep(0.05)                 def red1(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._mycanvas.delete('oval2')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval2')              canvas._tk.update()             time.sleep(0.05)                     def red2(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._mycanvas.delete('oval2')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval2')              canvas._mycanvas.delete('oval3')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval3')              canvas._tk.update()             time.sleep(0.05)                      def red3(a):         in range(a):             canvas._mycanvas.delete('oval1')             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval1')              canvas._mycanvas.delete('oval2')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval2')              canvas._mycanvas.delete('oval3')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval3')              canvas._mycanvas.delete('oval4')             canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval4')              canvas._tk.update()             time.sleep(0.05)                     def red4(a):         in range(a):             canvas._mycanvas.delete('oval5')             canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="red", tags = 'oval5')              canvas._tk.update()             time.sleep(0.05)          def green(a):         in range(a):             canvas._mycanvas.delete(tkinter.all)             canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval1')             canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval2')             canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval3')             canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval4')             canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="green", tags = 'oval5')             canvas._tk.update()             time.sleep(0.05)     def lights():         canvas._mycanvas.delete(tkinter.all)         canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracx,50*canvas.fracy,160*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval1')         canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracx,50*canvas.fracy,320*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval2')         canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracx,50*canvas.fracy,480*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval3')         canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracx,50*canvas.fracy,640*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval4')         canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracx,50*canvas.fracy,800*canvas.fracx,160*canvas.fracy, fill="black", tags = 'oval5')         canvas._tk.update()      # tag of drawn widgets     in range(20):         lights()         red0(30)         red1(30)         red2(30)         red3(30)         red4(30)         green(30)         lights()  if __name__ == "__main__":     main() 

so fix code , works now, check out! problem code though resized canvas in class resizingcanvas (which worked fine), in main() function, still keep creating new ovals inside resized canvas @ specific places.

moreover, think better if create whole tkinter application object since can control object more , making methods or attributes data. therefore, in fixed code, put whole tkinter.tk() object redraw() class. then, create ovals inside class , put tags each of them, can delete ovals want later on in main() function. code , ask me if don't understand it!

p/s: i'd prefer using .grid() instead of .pack() since better tool resizing windows , easier arrange interface!


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -