Python Forum
Centering and adding a push button to a grid window, TKinter - 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: Centering and adding a push button to a grid window, TKinter (/thread-40023.html)

Pages: 1 2


RE: Centering and adding a push button to a grid window, TKinter - menator01 - May-25-2023

I've corrected the code for you to look over.

import tkinter as tk
  
aList = ["Bin 1", "Bin 2", "Bin 3", "Bin 4", "Bin 5","Bin 6", "Bin 7", "Bin 8", "Bin 9", "Bin 10", "Bin 11",
        "Bin 12", "Bin 13", "Bin 14", "Bin 15"]
onList = ["Bin 3", "Bin 14"] #For test, usually empty

_alist = []
  
root = tk.Tk()
root['padx'] = 5
root['pady'] = 5
root.title('Feed Bins')
root.geometry("1200x900")
  
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
  
frame = tk.Frame(root)
frame['highlightbackground'] = 'black'
frame['highlightcolor'] = 'black'
frame['highlightthickness'] = 1
frame.grid(column=0, row=0, sticky='nsew', padx=5, pady=5)

i = 0
for x in range(5):
    frame.grid_rowconfigure(x, weight=3, uniform='rows')
    for y in range(3):
        frame.grid_columnconfigure(y, weight=3, uniform='cols')
        label = _alist.append(tk.Label(frame, text=aList[x+(y*5)], relief='raised', font=(None, 16, 'bold')))
        _alist[i]['bg'] = 'red' if _alist[i] in onList else 'gray86'
        _alist[i].grid(column=y, row=x, sticky='nsew', padx=5, pady=5)
        i += 1
  
 #check_alerts()
btn = tk.Button(root, text='Add item to onList', font=(None, 14, 'bold'), command = lambda: [onList.append("Bin 9"), print("Button Press")])
btn.grid(column=0, row=1)
  
def check_alerts():
    for x in range(0,len(_alist)):
       if _alist[x]['text'] in onList:
           _alist[x]['bg'] = 'red'
       else:
           _alist[x].config(bg = "lightgrey")
 
    root.after(1000,check_alerts)
    
root.after(1000, check_alerts) 
root.mainloop()



RE: Centering and adding a push button to a grid window, TKinter - deanhystad - May-25-2023

Post entire error message please, including the full traceback.

label is a Label, not a list. You have no way to reference all but the last Label.

You do not need the global keyword in check_alerts.


RE: Centering and adding a push button to a grid window, TKinter - Edward_ - May-25-2023

(May-25-2023, 11:53 AM)deanhystad Wrote: Post entire error message please, including the full traceback.

>>> %Run grid_centered_button.py
Traceback (most recent call last):
  File "/home/user/thonny_python/grid_centered_button.py", line 43, in <module>
    check_alerts()
  File "/home/user/thonny_python/grid_centered_button.py", line 40, in check_alerts
    label[x].config(bg = "lightgrey")
  File "/usr/lib/python3.9/tkinter/__init__.py", line 1652, in cget
    return self.tk.call(self._w, 'cget', '-' + key)
TypeError: can only concatenate str (not "int") to str



RE: Centering and adding a push button to a grid window, TKinter - Edward_ - May-25-2023

Thanks menator

Can you briefly explain why this code worked before trying to center the grid, without using the new _aList[] ?
And the benifit of using the _aList?
I'm just trying to understand better.
Thanks for your time.


RE: Centering and adding a push button to a grid window, TKinter - menator01 - May-25-2023

deanhystad gave the answer. You need to be able to reference the labels in the list. All I did was append the labels into a list and used it in the function.


RE: Centering and adding a push button to a grid window, TKinter - deanhystad - May-25-2023

Do you understand the error message now?

You got an error doing this:
label[x]
label is a tkinter Label object. Label objects support indexing, but you provide an attribute name as in label["bg"]. x is an int, not a str. Label objects don't understand indexing using ints. When Python tries to get the object associated with the index/key, it calls this.
return self.tk.call(self._w, 'cget', '-' + key)
Normally key would be a str, like "bg", but in your code it is an int (0). It cannot add 'cget' + 0. When you try to add an int and a str you get a type error.
Error:
TypeError: can only concatenate str (not "int") to str