Python Forum
[Tkinter] Grid the radio buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Grid the radio buttons
#1
Hi,

How can I position the radio buttons with with grid command and rows and col

import tkinter as tk

root = tk.Tk()

var_1 = tk.IntVar()
var_2 = tk.IntVar()

tk.Label(root, 
        text="""Radio Buttons""",
        justify = tk.LEFT,
        padx = 20).pack()

tk.Radiobutton(root, 
               text="A",
               padx = 20, 
               variable=var_1, 
               value=1).pack(anchor=tk.W)

tk.Radiobutton(root, 
               text="B",
               padx = 20, 
               variable=var_1, 
               value=2).pack(anchor=tk.W)


tk.Radiobutton(root, 
               text="C",
               padx = 20, 
               variable=var_2, 
               value=1).pack(anchor=tk.W)

tk.Radiobutton(root, 
               text="D",
               padx = 20, 
               variable=var_1, 
               value=2).pack(anchor=tk.W)

root.mainloop()
Yoriz write Nov-23-2021, 04:46 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
a) There is a special sub-forum for tkinter questions
b) You are packing now, where on the grid do you want them?

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Like so:
import tkinter as tk

root = tk.Tk()

var_1 = tk.IntVar()
var_2 = tk.IntVar()

tk.Label(root,
text="""Radio Buttons""",
justify = tk.LEFT).grid (row = 1, column = 1)

tk.Radiobutton(root,
text="A",
padx = 20,
variable=var_1,
value=1).grid (row = 2, column = 1)

tk.Radiobutton(root,
text="B",
padx = 20,
variable=var_1,
value=2).grid (row = 3, column = 1)


tk.Radiobutton(root,
text="C",
padx = 20,
variable=var_2,
value=1).grid (row = 2, column = 2)

tk.Radiobutton(root,
text="D",
padx = 20,
variable=var_1,
value=2).grid (row = 3, column = 2)

root.mainloop() 
Reply
#4
You cannot use pack() and grid() in the same frame, so make a frame for the radio buttons.
import tkinter as tk

root = tk.Tk()

var_1 = tk.IntVar(0)

tk.Label(root, text="""Radio Buttons""", justify=tk.LEFT, padx=20).pack()

radio_frame = tk.Frame(root)
radio_frame.pack()
for index, button in enumerate('ABCD'):
    button = tk.Radiobutton(radio_frame, text=button, variable=var_1, value=index)  # Buttons are added to radio_frame, not root
    button.grid(row=index // 2, column=index % 2, padx=5, pady=5)

root.mainloop()
Reply
#5
Hi, yes now I able to run this code having radio buttons which are arranged in rows and cols and are linked together. The buttons A and B are linked and have their value in same variable. Same is true for buttons C and D.

Now I need to print their their values when the radio button is selected but I it is not printing the values in the code below.

import tkinter as tk
 
root = tk.Tk()
 
var_ab = tk.IntVar()
var_cd = tk.IntVar()
 
tk.Label(root, 
        text="""Radio Buttons""",
        justify = tk.LEFT).grid (row = 1, column = 1)
 
tk.Radiobutton(root, 
               text="A",
               padx = 20, 
               variable=var_ab, 
               value=1).grid (row = 2, column = 1)
 
tk.Radiobutton(root, 
               text="B",
               padx = 20, 
               variable=var_ab, 
               value=2).grid (row = 2, column = 2)

tk.Radiobutton(root, 
               text="C",
               padx = 20, 
               variable=var_cd, 
               value=3).grid (row = 3, column = 1)

tk.Radiobutton(root, 
               text="D",
               padx = 20, 
               variable=var_cd, 
               value=4).grid (row = 3, column = 2)

print("The Variable AB = " , var_ab)
print("The Variable CD = " , var_cd)

root.mainloop() 
Reply
#6
#! /usr/bin/env python3.10
import tkinter as tk

root = tk.Tk()

def show():
    print(f' Variable AB -> {var_ab.get()}')
    print(f' Variable CD -> {var_cd.get()}')

var_ab = tk.IntVar()
var_cd = tk.IntVar()

tk.Label(root,
        text="""Radio Buttons""",
        justify = tk.LEFT).grid (row = 1, column = 1)

tk.Radiobutton(root,
               text="A",
               padx = 20,
               variable=var_ab,
               command=show,
               value=1).grid (row = 2, column = 1)

tk.Radiobutton(root,
               text="B",
               padx = 20,
               variable=var_ab,
               command=show,
               value=2).grid (row = 2, column = 2)

tk.Radiobutton(root,
               text="C",
               padx = 20,
               variable=var_cd,
               command=show,
               value=3).grid (row = 3, column = 1)

tk.Radiobutton(root,
               text="D",
               padx = 20,
               variable=var_cd,
               command=show,
               value=4).grid (row = 3, column = 2)



root.mainloop()
Output:
Variable AB -> 1 Variable CD -> 0 Variable AB -> 1 Variable CD -> 4 Variable AB -> 1 Variable CD -> 3 Variable AB -> 2 Variable CD -> 3
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#7
One more example

#! /usr/bin/env python3
import tkinter as tk
from functools import partial

class Window:
    def __init__(self, parent):
        parent.columnconfigure(0, weight=1)
        my_list = ['orange', 'red', 'blue', 'pink', 'green', 'yellow', 'gold', 'silver',
                   'violet', 'magenta', 'gray', 'burlywood', 'tan', 'brown', 'slateblue',
                   'snow', 'dodgerblue', 'royalblue', 'steelblue', 'mediumblue', 'darkblue',
                   'limegreen', 'cornflowerblue', 'wheat', 'powderblue', 'purple', 'cyan',
                   'forestgreen', 'tomato', 'cornsilk']
        self.fg_list = ['blue', 'mediumblue', 'darkblue', 'purple']

        self.label = tk.Label(parent, text=' ', pady=5)
        self.label['borderwidth'] = 1
        self.label['highlightthickness'] = 1
        self.label['highlightcolor'] = 'black'
        self.label['highlightbackground'] = 'black'
        self.label['bg'] = 'orange'
        self.label.grid(column=0, row=0, columnspan=5,sticky='news', pady=5)

        var = tk.StringVar()
        var.set('orange')

        col = 0
        row = 1
        for option in my_list:
            radio = tk.Radiobutton(parent, bg=option, bd=1, highlightbackground='black', anchor='w')
            if option in self.fg_list:
                radio['fg'] = 'snow'
            else:
                radio['fg'] = 'black'
            radio['variable'] = var
            radio['value'] = option
            radio['text'] = option.title()
            radio['tristatevalue'] = option
            radio['command'] = partial(self.show, var)
            radio.grid(column=col, row=row, sticky='new', padx=1, pady=1)
            radio.grid_columnconfigure(col, weight=3, uniform='columns')
            if col >= 4:
                col = 0
                row += 1
            else:
                col += 1
        self.show('Orange')

    def show(self, var):
        if type(var) == str:
            self.label['text'] = var.title()
            self.label['bg'] = var
        else:
            self.label['text'] = var.get().title()
            self.label['bg'] = var.get()
            if var.get() in self.fg_list:
                self.label['fg'] = 'snow'
            else:
                self.label['fg'] = 'black'




def main():
    root = tk.Tk()
    root['padx'] = 5
    root['pady'] = 5
    Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 414 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  problem with radio button crook79 3 714 Aug-12-2021, 02:30 PM
Last Post: deanhystad
  .grid buttons AnunnakiKungFu 3 731 Feb-08-2021, 05:56 PM
Last Post: deanhystad
  [Tkinter] I can't get information from a radio button aquerci 2 1,448 May-20-2020, 10:31 AM
Last Post: aquerci
  [Tkinter] Radio button help Muzz 5 1,972 Apr-28-2019, 07:43 AM
Last Post: Muzz

Forum Jump:

User Panel Messages

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