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
#4
Sorry, I answered the wrong question again.

To place a frame in the middle of the window, use "frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)".
import tkinter as tk
import random


class FramedLabel(tk.Frame):
    """A label with a frame.  Looks suspiciously like a button."""
    def __init__(
            self,
            parent,
            text="",
            relief=tk.RAISED,
            borderwidth=4,
            font=(None, 10),
            width=10,
            height=3,
            **kwargs):
        super().__init__(
            parent, relief=relief, borderwidth=borderwidth, **kwargs
        )
        self.label = tk.Label(
            self, text=text, font=font, width=width, height=height
        )
        self.label.pack()

    def set_color(self, color):
        self["bg"] = color
        self.label["bg"] = color


class MyWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Feed Bins")
        self.geometry("800x400")

        frame = tk.Frame(self)
        frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
        self.labels = [
            FramedLabel(frame, text=f"Bin {x}") for x in range(1, 16)
        ]
        for x, label in enumerate(self.labels):
            label.grid(row=x // 5, column=x % 5, padx=5, pady=5)
            label.set_color("lightgrey")

        button = tk.Button(frame, text="Update", command=self.check_alerts)
        button.grid(row=4, column=0, columnspan=5, sticky="news")

    def check_alerts(self):
        for label in self.labels:
            label.set_color(random.choice(("red", "lightgrey")))

 
MyWindow().mainloop() 
However, I prefer to pack the frame and set the borders to expand the window to the desired size.
import tkinter as tk
import random


class FramedLabel(tk.Frame):
    """A label with a frame.  Looks suspiciously like a button."""
    def __init__(
            self,
            parent,
            text="",
            relief=tk.RAISED,
            borderwidth=4,
            font=(None, 10),
            width=10,
            height=3,
            **kwargs):
        super().__init__(
            parent, relief=relief, borderwidth=borderwidth, **kwargs
        )
        self.label = tk.Label(
            self, text=text, font=font, width=width, height=height
        )
        self.label.pack()

    def set_color(self, color):
        self["bg"] = color
        self.label["bg"] = color


class MyWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Feed Bins")

        frame = tk.Frame(self)
        frame.pack(padx=100, pady=100)
        self.labels = [
            FramedLabel(frame, text=f"Bin {x}") for x in range(1, 16)
        ]
        for x, label in enumerate(self.labels):
            label.grid(row=x // 5, column=x % 5, padx=5, pady=5)
            label.set_color("lightgrey")

        button = tk.Button(frame, text="Update", command=self.check_alerts)
        button.grid(row=4, column=0, columnspan=5, sticky="news")

    def check_alerts(self):
        for label in self.labels:
            label.set_color(random.choice(("red", "lightgrey")))

 
MyWindow().mainloop() 
Either way, the real trick is to create a frame that holds your labels and buttons. Pack (or grid) the labels and buttons in the frame. Place or pack the frame in the window
Reply


Messages In This Thread
RE: Centering and adding a push button to a grid window, TKinter - by deanhystad - May-22-2023, 12:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter multiple windows in the same window hosierycouch 1 209 May-30-2024, 04:28 AM
Last Post: deanhystad
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 951 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 1,446 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 1,116 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 1,068 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 2,121 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 3,124 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,349 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,717 Feb-17-2022, 07:02 AM
Last Post: pymn
  Can't get tkinter button to change color based on changes in data dford 4 3,604 Feb-13-2022, 01:57 PM
Last Post: dford

Forum Jump:

User Panel Messages

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