Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Padding between widgets that was set by tkinter's .grid() method, even though I didn't set it

I tried place widgets by .grid() method, and when I place RadioButtons on different row – I saw that there padding on different row, also between them.

This is what I talking about (I point paddings that I noticed):
enter image description here

And there my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import tkinter as tk
from string import hexdigits
from tkinter import ttk
from random import choices

color_hex = '#C91212'

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Main window')
        self.tool = tk.IntVar(value=0)
        self.topmenu = TopMenu(self)
        self.topmenu.pack()

class TopMenu(tk.Frame):
    def __init__(self, master: MainWindow):
        tk.Frame.__init__(self, master)
        self.root = master
        ttk.Label(self, text="Hex color:").grid(row=0, column=0)

        self.symbol_entry_text = tk.StringVar(value=color_hex)
        self.symbol_entry = ttk.Entry(self, textvariable=self.symbol_entry_text)
        self.symbol_entry.grid(row=0, column=1)

        ttk.Button(self, text='Random').grid(column=3, row=0)
        ttk.Button(self, text='Load').grid(column=4, row=0)
        ttk.Button(self, text='Save').grid(column=5, row=0)

        self.c = tk.Canvas(self, width=21, height=21, highlightthickness=0, bg=color_hex)
        self.c.grid(column=2, row=0)
        
        ttk.Radiobutton(self, text='Pencil', value=0, variable=self.root.tool).grid(row=1, column=0)
        ttk.Radiobutton(self, text='Floodfill', value=1, variable=self.root.tool).grid(row=1, column=1)
        ttk.Radiobutton(self, text='Eyedropper', value=2, variable=self.root.tool).grid(row=1, column=2)

if __name__ == "__main__":
    app = MainWindow()
    app.mainloop()

>Solution :

There is no problem with your code, The thing here is that the color Canvas and the Eyedropper radio box is in the same column (column=2), that means the column width will we same for the both element.

Now there are some ways to get the window as you want

For this type of design

enter image description here

You can change the coulmnspan of Eyedropper radio button to 2 it means that now the radio button will we in two column.

ttk.Radiobutton(self, text='Eyedropper', value=2, variable=self.root.tool).grid(row=1, column=2, columnspan=2)

Full code

import tkinter as tk
from string import hexdigits
from tkinter import ttk
from random import choices

color_hex = '#C91212'

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Main window')
        self.tool = tk.IntVar(value=0)
        self.topmenu = TopMenu(self)
        self.topmenu.pack()

class TopMenu(tk.Frame):
    def __init__(self, master: MainWindow):
        tk.Frame.__init__(self, master)
        self.root = master
        ttk.Label(self, text="Hex color:").grid(row=0, column=0, padx=10)

        self.symbol_entry_text = tk.StringVar(value=color_hex)
        self.symbol_entry = ttk.Entry(self, textvariable=self.symbol_entry_text)
        self.symbol_entry.grid(row=0, column=1)

        ttk.Button(self, text='Random').grid(column=3, row=0, padx=10)
        ttk.Button(self, text='Load').grid(column=4, row=0)
        ttk.Button(self, text='Save').grid(column=5, row=0, padx=10)

        self.c = tk.Canvas(self, width=21, height=21, highlightthickness=0, bg=color_hex)
        self.c.grid(column=2, row=0)
        
        ttk.Radiobutton(self, text='Pencil', value=0, variable=self.root.tool).grid(row=1, column=0)
        ttk.Radiobutton(self, text='Floodfill', value=1, variable=self.root.tool).grid(row=1, column=1)
        ttk.Radiobutton(self, text='Eyedropper', value=2, variable=self.root.tool).grid(row=1, column=2, columnspan=2) # see here

if __name__ == "__main__":
    app = MainWindow()
    app.mainloop()

Or for this
enter image description here

you can add sticky="w" (w for west) in Canvas griding

self.c.grid(column=2, row=0, sticky="w")

Full Code

import tkinter as tk
from string import hexdigits
from tkinter import ttk
from random import choices

color_hex = '#C91212'

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Main window')
        self.tool = tk.IntVar(value=0)
        self.topmenu = TopMenu(self)
        self.topmenu.pack()

class TopMenu(tk.Frame):
    def __init__(self, master: MainWindow):
        tk.Frame.__init__(self, master)
        self.root = master
        ttk.Label(self, text="Hex color:").grid(row=0, column=0, padx=10)

        self.symbol_entry_text = tk.StringVar(value=color_hex)
        self.symbol_entry = ttk.Entry(self, textvariable=self.symbol_entry_text)
        self.symbol_entry.grid(row=0, column=1)

        ttk.Button(self, text='Random').grid(column=3, row=0, padx=10)
        ttk.Button(self, text='Load').grid(column=4, row=0)
        ttk.Button(self, text='Save').grid(column=5, row=0, padx=10)

        self.c = tk.Canvas(self, width=21, height=21, highlightthickness=0, bg=color_hex)
        self.c.grid(column=2, row=0, sticky="w")
        
        ttk.Radiobutton(self, text='Pencil', value=0, variable=self.root.tool).grid(row=1, column=0)
        ttk.Radiobutton(self, text='Floodfill', value=1, variable=self.root.tool).grid(row=1, column=1)
        ttk.Radiobutton(self, text='Eyedropper', value=2, variable=self.root.tool).grid(row=1, column=2)

if __name__ == "__main__":
    app = MainWindow()
    app.mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading