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):

And there my 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)
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
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()
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()

