#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2017
    Posts
    31
    Rep Power
    1

    Unhappy which button should be shown


    suppose we have a lot of buttons with specified texts & locations... for example our buttons are {'main','report', 'memory', 'bills',...} now we have an array that determine which button should be visible for example z1=['report', 'bills'] the contents of this array can be the other buttons in your oponions, how can i write the code? should i compare the text of every button with "z1" array, before defining every button this is not reasonable,because the numbers of buttons are alot please helppppppp Confused
    this is mu code
    Code:
    	z1=['report','bills','setting']
    	
    	self.button = Button(self.master,text="memory", command=self.ouvrir)
            self.button.grid(row=1, column=1)
            
            self.button2 = Button(self.master,text="report", command=self.tabluh)
            self.button2.grid(row=1, column=2)
    
    
            self.button3 = Button(self.master,text="setting", command=self.new_window)
            self.button3.grid(row=1, column=3)
    
            self.button4 = Button(self.master,text="calibration", command=self.cal)
            self.button4.grid(row=1, column=4)
    
    
            self.button5 = Button(self.master,text="empty weight", command=self.weighing)
            self.button5.grid(row=1, column=5)
    
            self.button6 = Button(self.master,text="bills", command=self.history)
            self.button6.grid(row=1, column=6) 
    
            self.button7 = Button(self.master,text="full weight", command=self.full)
            self.button7.grid(row=1, column=7)]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    653
    Rep Power
    39
    I don't understand your question, so, how does the program know what button should be visible? Generally, the code or function that knows buttonX should be visible would first call a common function that calls grid-forget() on all buttons. It would then grid that specific button, say self.button5, so it is the only one visible.
    Last edited by dwblas; March 30th, 2017 at 02:56 PM.
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    Did you want to reconfigure the layout or just disable some buttons?

    If merely disabling perhaps run a loop like this:
    ....but first, store all the buttons in a list. Let's call it buttons.
    Code:
    states = (tkinter.DISABLED, tkinter.NORMAL,)
    
    set_z1 = set(z1)  # sets compare efficiently
    
    for button in self.buttons:
        configuration = button.config()
    
        # I do not know what are the elements of the text item tuple, you probably need the last one.
        button_text = configuration['text'][-1]
    
        button.config(state = states[button_text in set_z1]
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo