user12170100
user12170100

Reputation:

Tkinter widgets in frame

I have a LabelFrame that has columnspan of 3. I would like the 2 Checkbutton widgets in the frame to be on either side. How can I grid the widgets accordingly?

I tried doing sticky='e' and sticky='w' but it is not doing what I want it to.

frame = tk.LabelFrame(window, text='Tools', font=('Helvetica', 15), fg='white',
                                        bg='#022340')
frame.grid(row=3, column=0, sticky='ew', columnspan=3)

button1 = tk.Checkbutton(frame, text='Button1', font=('Helvetica', 15, 'bold'),bg='red')
button2 = tk.Checkbutton(frame, text='Button2', font=('Helvetica', 15, 'bold'),bg='yellow')

button1.grid(row=0, column=1, sticky='w')
button2.grid(row=0, column=0, sticky='e')

This is what I'm looking for: Format that I want

Upvotes: 0

Views: 80

Answers (1)

Somraj Chowdhury
Somraj Chowdhury

Reputation: 1043

I have placed a Label widget with some width and no text in between the two checkbuttons and gave it the same background as that of the root window.

I have used the grid_rowconfigure() and grid_columnconfigure() for the window and the frame to make the GUI responsive.

I have added a Label widget at the bottom of the GUI so that you can have an idea of how to add widgets below it if your app has more than the two checkbutton widgets.

import tkinter as tk

window = tk.Tk()

frame = tk.LabelFrame(window, text='Tools', font=('Helvetica', 15), fg='white', bg='#022340')
frame.grid(row=0, column=0, sticky='nsew')

window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)

frame.grid_rowconfigure((0,1), weight=1)
frame.grid_columnconfigure((1), weight=1)

button1 = tk.Checkbutton(frame, text='Button1', font=('Helvetica', 15, 'bold'), bg='red')
button2 = tk.Checkbutton(frame, text='Button2', font=('Helvetica', 15, 'bold'), bg='yellow')

# the label widget between the two checkbuttons
label_m = tk.Label(frame, width=30, bg='#022340')
label_m.grid(row=0, column=1)

# Add some padding to separate them apart
button1.grid(row=0, column=2, sticky='nsew')
button2.grid(row=0, column=0, sticky='nsew')

# a label widget to show how all widgets are placed in GUI
label = tk.Label(frame, text='A Label Widget', bg='white')
label.grid(row=1, sticky='nswe', columnspan=3)

window.mainloop()

Screenshots

g1

g2

g3

Hope this helps!

Upvotes: 1

Related Questions