Python Forum
[Tkinter] optimalization of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] optimalization of code
#1
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 Confused

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)
Reply
#2
Create the Checkbuttons in a loop, store the Checkbutton handles in a lists, loop through the lists to update the Checkbuttons.
Reply
#3
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
Reply
#4
If you want access by row and column make a list of lists
https://python-forum.io/Thread-Basic-Lists Wrote:Matrices
You can represent matrices (in C: multidimensional arrays) with lists also, by nesting lists inside of lists.
>>> matrix = [[1,2,3], [4,5,6], [7,8,9]]
>>> matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> matrix[0]
[1, 2, 3]
>>> matrix[0][0]
1
>>> matrix[-1]
[7, 8, 9]
>>> matrix[-1][0]
7
Reply
#5
(Aug-07-2019, 05:42 AM)Yoriz Wrote: If you want access by row and column make a list of lists
https://python-forum.io/Thread-Basic-Lists Wrote:Matrices
You can represent matrices (in C: multidimensional arrays) with lists also, by nesting lists inside of lists.
>>> matrix = [[1,2,3], [4,5,6], [7,8,9]]
>>> matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> matrix[0]
[1, 2, 3]
>>> matrix[0][0]
1
>>> matrix[-1]
[7, 8, 9]
>>> matrix[-1][0]
7

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)
Reply
#6
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?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020