Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add column headings with TKinter grid and centering

Status
Not open for further replies.

Aaron5

Programmer
Apr 10, 2007
25
0
0
US
I'm learning TKinter slowly and trying to use it for a small home project.

I have a grid filled from a Python list, that is working, but I would like to add a top row with column headings, and force the grid to be centered in the window. I have created grids with manually coded rows, having text headings in row 0, but in my grid based on a list I haven't been able to use Row 0 for label text.

As I can learn, I would also like to add (smaller font) centered text line at the bottom of the window, and maybe a push button or two at far right lower corner. See code below.

Python:
import tkinter as tk
import tkinter.font as font
window = tk.Tk()
window.title("Feed Bins")
window.geometry("800x400")

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 12", "Bin 14"]

myFont2 = font.Font(size=10)

label= [0] * 15
for x in range(5):    # Number of Rows
   for y in range(3): #Number of Colums
       frame = tk.Frame(master=window,relief=tk.RAISED,borderwidth=4)
       frame.grid(row=x, column=y, padx=5, pady=5)  # line 13
       label[x+(y*5)] = tk.Label(master=frame, text=aList[x+(y*5)],font=myFont2, height=3, width=20)
       label[x+(y*5)].pack()

def check_alerts():
   global aList,onList
   for x in range(0,len(aList)):
       if aList[x] in onList:
           label[x].config(bg = "red")
       else:
           label[x].config(bg = "lightgrey")
   window.after(1000,check_alerts)
   
check_alerts()

window.mainloop()

Any advice greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top