Hi
I am running a simple application which has three Radio Button and I have two problem with that as following:
1- First of all, two of the radio buttons [R2(Blue) and R3(Green)] are automatically selected when I run the code!(As you can see in attached screen shot). I tried to use a method like this:

self.R2.deselect()
self.R3.deselect()

but they didn't change any thing.
2- The second problem is the size of my second frame(here fm).How I can dock the fm frame to expand all of parent frame?if you take a look at the second attachment you will see when ever I click on the radio button it just change the fm bg color as:

self.fm.configure(bg="BLUE")

an this just make changes on the portion of the radio buttons not all main frame.is this because i am using the grid layout management?If so? How can I solve it?

Here is my code:

from Tkinter import *

class App:
   def __init__(self, parent):
    self.myParent = parent
    self.fm = Frame(parent, width=400, height=300)
    self.fm.pack_propagate(0) # to keep the frame size
    self.fm.pack()


    self.R1 = Radiobutton(self.fm, text="Red", value=1, command=self.make_Red)
    self.R1.grid(row =2 , column = 1)

    self.R2 = Radiobutton(self.fm, text="Blue",  value=2, command=self.make_Blue)
    self.R2.grid(row =2 , column = 2)

    self.R3 = Radiobutton(self.fm, text="Green", value=3, command=self.make_Green)
    self.R3.grid(row =2 , column = 3)


   def make_Red(self):
         self.fm.configure(bg="RED")
         self.R1.configure(bg="RED")
         self.R2.configure(bg="RED")
         self.R3.configure(bg="RED")

   def make_Blue(self):
        self.fm.configure(bg="BLUE")
        self.R1.configure(bg="BLUE")
        self.R2.configure(bg="BLUE")
        self.R3.configure(bg="BLUE")

   def make_Green(self):
        self.fm.configure(bg="GREEN")
        self.R1.configure(bg="GREEN")
        self.R2.configure(bg="GREEN")
        self.R3.configure(bg="GREEN")

root = Tk()
root.title ("Color Option")
app = App(root)
root.mainloop()

You can't combine grid() and pack() but must use one or the other. Also, AFAIK, one radio button must be selected (a default selection or you can't choose 'none' when using radio buttons), and the others can be deselected.

from Tkinter import *
 
class App:
   def __init__(self, parent):
    self.my_parent = parent
    self.my_parent.geometry("200x100+10+10")
#    self.fm = Frame(parent, width=400, height=300)
#    self.fm.pack_propagate(0) # to keep the frame size
#    self.fm.pack()
 
 
    self.R1 = Radiobutton(self.my_parent, text="Red", value=1, command=self.make_Red)
    self.R1.grid(row =2 , column = 1)
    self.R1.select()

    self.R2 = Radiobutton(self.my_parent, text="Blue",  value=2, command=self.make_Blue)
    self.R2.grid(row =2 , column = 2)
    self.R2.deselect()
 
    self.R3 = Radiobutton(self.my_parent, text="Green", value=3, command=self.make_Green)
    self.R3.deselect()
    self.R3.grid(row =2 , column = 3)
 
 
   def change_buttons(self, color):
       self.my_parent.configure(bg=color)
       self.R1.configure(bg=color)
       self.R2.configure(bg=color)
       self.R3.configure(bg=color)

   def make_Red(self):
       self.change_buttons("Red")
 
   def make_Blue(self):
       self.change_buttons("BLUE")
 
   def make_Green(self):
       self.change_buttons("GREEN")
 
root = Tk()
root.title ("Color Option")
app = App(root)
root.mainloop()
commented: Tkinter Guru Award. +5

You can't combine grid() and pack() but must use one or the other. Also, AFAIK, one radio button must be selected (a default selection or you can't choose 'none' when using radio buttons), and the others can be deselected.

from Tkinter import *
 
class App:
   def __init__(self, parent):
    self.my_parent = parent
    self.my_parent.geometry("200x100+10+10")
#    self.fm = Frame(parent, width=400, height=300)
#    self.fm.pack_propagate(0) # to keep the frame size
#    self.fm.pack()
 
 
    self.R1 = Radiobutton(self.my_parent, text="Red", value=1, command=self.make_Red)
    self.R1.grid(row =2 , column = 1)
    self.R1.select()

    self.R2 = Radiobutton(self.my_parent, text="Blue",  value=2, command=self.make_Blue)
    self.R2.grid(row =2 , column = 2)
    self.R2.deselect()
 
    self.R3 = Radiobutton(self.my_parent, text="Green", value=3, command=self.make_Green)
    self.R3.deselect()
    self.R3.grid(row =2 , column = 3)
 
 
   def change_buttons(self, color):
       self.my_parent.configure(bg=color)
       self.R1.configure(bg=color)
       self.R2.configure(bg=color)
       self.R3.configure(bg=color)

   def make_Red(self):
       self.change_buttons("Red")
 
   def make_Blue(self):
       self.change_buttons("BLUE")
 
   def make_Green(self):
       self.change_buttons("GREEN")
 
root = Tk()
root.title ("Color Option")
app = App(root)
root.mainloop()

Don't forget to refactor the code when there are sequences of similar widgets

from Tkinter import *
from functools import partial
 
class App:
   def __init__(self, parent):
    self.my_parent = parent
    self.my_parent.geometry("200x100+10+10")
 
    self.R = list()
    for i, color in enumerate(("Red", "Blue", "Green")):
        btn = Radiobutton(self.my_parent, text=color, value=i+1, command = partial(self.change_buttons, color))
        btn.grid(row = 2, column = i+1)
        btn.deselect()
        self.R.append(btn)
    self.R[0].select()
 
 
   def change_buttons(self, color):
       self.my_parent.configure(bg=color)
       for btn in self.R:
            btn.configure(bg=color)

if __name__ == "__main__":
    root = Tk()
    root.title ("Color Option")
    app = App(root)
    root.mainloop()

An improvement in the previous code is to replace self.R[0].select() by self.R[0].invoke() which invokes the button's action. You can even remove the call to deselect() .

commented: Perfect +1

Gribouillis and woooee
Thanks again and sorry for late reply I was out of town for weekend
I understand how call_back works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.