Home > Back-end >  How to customize position of checkboxes, drop down list, etc. in Python tkinter?
How to customize position of checkboxes, drop down list, etc. in Python tkinter?

Time:06-16

I want to achieve the exact appearance of this image

I want to learn how to position the tickboxes, checkbox, dropdown menu, and menu list that exactly looks like the image that I have inserted. I do not know the syntax on how to write the positioning of each

from tkinter import *

from tkinter import ttk

windows = Tk()
gender = ["male", "female"]
sport = ["Cricket", "Tennis"]
numbers = ['one', 'two', 'three', 'four']

windows.title("Hello Python")
windows.geometry("350x300")

x = IntVar(windows)
for index in range(len(gender)):
    radiobutton = Radiobutton(windows,
                              text=gender[index],
                              variable=x,
                              value=index,)
    radiobutton.pack(side=LEFT,ipadx=10,ipady=10)


for index in range(len(sport)):
    checkboxgui = Checkbutton(windows,
                               text=sport[index], onvalue=1,
                               offvalue=0)

    checkboxgui.pack(side=LEFT, anchor=CENTER, ipadx=10, ipady=10)

clicked = StringVar(windows)
clicked.set("one")
drop = ttk.Combobox(windows, textvariable=clicked,values=numbers)
drop.pack()

listbox = Listbox(windows,selectmode=MULTIPLE)
listbox.pack()
listbox.insert(1,"one")
listbox.insert(2,"two")
listbox.insert(3,"three")
listbox.insert(4,"four")
listbox.config(height=listbox.size())



windows.mainloop()

CodePudding user response:

The only way I know how to achieve something like your picture is by organizing the interactive widgets into frames and then pack() them individually so that you can follow the logic the packer will use. But I did set an approximate size like your example. Notice that in order to get your Comboboxto align at the top I had to use the expand and fill parameter. It would be beneficial for you to look at something like this. For a nice explanation with examples for how the pack() method works. I did not prevent the user from expanding the window, so you could still maximize the window beyond what you showed in your picture. I hope this helps.

import tkinter as tk
from tkinter import ttk

window = tk.Tk()
window.geometry("400x300")

x = tk.IntVar()
numbers = ['one', 'two', 'three', 'four']

radio_frame = tk.Frame(window)
radio_frame.pack(ipady=30)

male_radio = tk.Radiobutton(radio_frame, text="male", variable=x)
male_radio.pack(side="left")

female_radio = tk.Radiobutton(radio_frame, text="female", variable=x)
female_radio.pack(side="left")

check_frame = tk.Frame(window)
check_frame.pack(ipady=10)

check_button1 = tk.Checkbutton(check_frame, text="Cricket", onvalue=1, offvalue=0)
check_button1.pack(side="left")

check_button2 = tk.Checkbutton(check_frame, text="Tennis", onvalue=1, offvalue=0)
check_button2.pack(side="left")

bottom_frame = tk.Frame(window)
bottom_frame.pack(pady=20)

drop_frame = tk.Frame(bottom_frame)
drop_frame.pack(side="left", expand=True, fill=tk.BOTH)

clicked = tk.StringVar()
clicked.set("one")

drop = ttk.Combobox(drop_frame, textvariable=clicked, values=numbers)
drop.pack()

list_frame = tk.Frame(bottom_frame)
list_frame.pack(side="left")

listbox = tk.Listbox(list_frame,selectmode=tk.MULTIPLE)
listbox.pack(padx=40, ipady=10)

listbox.insert(1,"one")
listbox.insert(2,"two")
listbox.insert(3,"three")
listbox.insert(4,"four")
listbox.config(height=listbox.size())

window.mainloop()
  • Related