[Tkinter] Grid the radio buttons - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Grid the radio buttons (/thread-35622.html) |
Grid the radio buttons - Joni_Engr - Nov-23-2021 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() RE: Grid the radio buttons - DPaul - Nov-23-2021 a) There is a special sub-forum for tkinter questions b) You are packing now, where on the grid do you want them? Paul RE: Grid the radio buttons - BashBedlam - Nov-23-2021 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() RE: Grid the radio buttons - deanhystad - Nov-23-2021 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() RE: Grid the radio buttons - Joni_Engr - Nov-24-2021 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() RE: Grid the radio buttons - menator01 - Nov-24-2021 #! /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()
RE: Grid the radio buttons - menator01 - Nov-24-2021 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() |