Python Forum
Centering and adding a push button to a grid window, TKinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Centering and adding a push button to a grid window, TKinter
#9
I have one more example. Doesn't use the button to update. uses after. Sometimes pulls the same bin multiple times in the comparison list though.
So instead of three red labels, will get only one or two.

import tkinter as tk
from random import choices

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"]


def checker(root, labels):
    bins = choices(aList, k=3)
    print(bins)
    for label in labels:
        label['bg'] = 'red' if label['text'] in bins else 'gray86'
    root.after(1000, lambda: checker(root, labels))


root = tk.Tk()
root['padx'] = 5
root['pady'] = 5
root.title('Feed Bins')

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='news', padx=5, pady=5)

_labels = []
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')
        _labels.append(tk.Label(frame, text=aList[x+(y*5)], relief='raised', font=(None, 16, 'bold')))
        _labels[i]['bg'] = 'gray86'
        _labels[i].grid(column=y, row=x, sticky='news', padx=5, pady=5)
        i+=1


btn = tk.Button(root, text='Button', font=(None, 14, 'bold'))
btn.grid(column=0, row=1, pady=8)
root.after(1000, lambda: checker(root, _labels))
root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Messages In This Thread
RE: Centering and adding a push button to a grid window, TKinter - by menator01 - May-24-2023, 06:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter Radio button group not setting default selection simonc88 3 1,511 Mar-20-2025, 06:53 PM
Last Post: buran
  Trying to update label text using a grid button. Edward_ 7 2,097 Dec-18-2024, 03:05 AM
Last Post: Edward_
  Using place for a button, many examples but AttributeError, tkinter Edward_ 3 1,197 Dec-17-2024, 09:06 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window hosierycouch 1 1,377 May-30-2024, 04:28 AM
Last Post: deanhystad
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 2,899 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 5,356 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 2,930 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 2,328 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 3,675 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 5,464 Apr-16-2022, 04:04 PM
Last Post: DBox

Forum Jump:

User Panel Messages

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