[Tkinter] optimalization of code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] optimalization of code (/thread-20358.html) |
optimalization of code - storzo - Aug-06-2019 Hi guys can someone help me with optimalization of this code? i got 5 chechk box (named Px) and 5 other (named Rx) I wish: on begin all check box got status NORMAL, when we mark P1 we can't mark R1 (status DISABLED)- same thing relation between P2-R2, P3-R3 ... in two ways. In def confirm i wrote algorythm but i think it's possible to make short way. one think is wrong when we mark R1 at first then we mark P1, R1 got status DISABLED but mark will stay on it(dot). example : http://prntscr.com/op9ydv import numpy as np s=np.zeros((10),dtype=object) ... self.checkvars = [IntVar() for i in range(10)] self.chk1 = Checkbutton(root, text='P1', var=self.checkvars[0], command=self.confirm) self.chk2 = Checkbutton(root, text='P2', var=self.checkvars[1], command=self.confirm)... self.chk11 = Checkbutton(root, text='R1', variable=self.checkvars[5], command=self.confirm) self.chk12 = Checkbutton(root, text='R2', variable=self.checkvars[6], command=self.confirm)... def confirm(self): s[:] = [1 if v.get() else 0 for v in self.checkvars] if s[0] == 1: s[5]=0 self.chk11.config(state=DISABLED) if s[0] == 0: self.chk11.config(state=NORMAL) RE: optimalization of code - Yoriz - Aug-07-2019 Create the Checkbuttons in a loop, store the Checkbutton handles in a lists, loop through the lists to update the Checkbuttons. RE: optimalization of code - storzo - Aug-07-2019 I think is bad idea crating in lists. Check box are not only one var in my program i got almost 40 var selected by category. List is not array, and acces must be organizate by row column RE: optimalization of code - Yoriz - Aug-07-2019 If you want access by row and column make a list of lists https://python-forum.io/Thread-Basic-Lists Wrote:Matrices RE: optimalization of code - storzo - Aug-07-2019 (Aug-07-2019, 05:42 AM)Yoriz Wrote: If you want access by row and column make a list of lists yes is different option: how to store data, but biggest problem is in disabled and normal status- i focus on that. - how to make in nice easy way than that: def confirm(self): s[:] = [1 if v.get() else 0 for v in self.checkvars] if s[0] == 1: s[5]=0 self.chk11.config(state=DISABLED) if s[0] == 0: self.chk11.config(state=NORMAL) RE: optimalization of code - storzo - Aug-07-2019 i try like this something short: def confirm(self): s[:] = [1 if v.get() else 0 for v in self.checkvars] self.chk1.config(state=DISABLED if s[5] == 1 else NORMAL ) self.chk2.config(state=DISABLED if s[6] == 1 else NORMAL) self.chk3.config(state=DISABLED if s[7] == 1 else NORMAL ) self.chk4.config(state=DISABLED if s[8] == 1 else NORMAL) self.chk5.config(state=DISABLED if s[9] == 1 else NORMAL ) self.chk11.config(state=DISABLED if s[0] == 1 else NORMAL) self.chk12.config(state=DISABLED if s[1] == 1 else NORMAL) self.chk13.config(state=DISABLED if s[2] == 1 else NORMAL) self.chk14.config(state=DISABLED if s[3] == 1 else NORMAL) self.chk15.config(state=DISABLED if s[4] == 1 else NORMAL)any suggestion? |