Python Forum
[Tkinter] Ratiobutton default value in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Ratiobutton default value in a function
#1
I want to add a radiobutton that have a default value on my gui (switching between 'digital' and 'analog'). Therefore, I added the following lines

    tk.Label(root, text = 'MPD').grid(row = 1, column = 15)
    MPDmode = tk.Frame(root)
    MPDmode.grid(row = 2, column = 15)
    MPDmode_var = tk.StringVar(value = 'digital')
    MPDdigital = tk.Radiobutton(MPDmode, text = 'Digital', variable = MPDmode_var, indicatoron = False, value = 'digital', width = 8)
    MPDanalog = tk.Radiobutton(MPDmode, text = 'Analog', variable = MPDmode_var, indicatoron = False, value = 'analog', width = 8)
    MPDdigital.grid(row = 0)
    MPDanalog.grid(row = 1)
When I run this individually, it works well. However, if I define the above in a function.

def startGUI():
'''The above stuff'''
and run it as
root = tk.Tk()
startGUI()
root.mainloop()
The radiobutton takes nothing as the default value. I wonder how I can solve it, thanks!
Reply
#2
yo,
you found a quirky thing. it works out of a function but not in it. Well you have missed the most important attribute of a radio button and that's the command attribute.
Without a command your just pressing buttons with no meaning.
you also don't need to a give you string var a value but you need to set it,,,
MPDmode_var = tk.StringVar()
MPDdigital = tk.Radiobutton(MPDmode, text = 'Digital', variable = MPDmode_var, indicatoron = False, value = 'digital', width = 8, command= dosomething)
MPDmode_var.set('digital')
finally it's ok to practice but learn to use classes:
from tkinter import *
class MyButton(Frame):
    def __init__(self, parent=None):
        self.parent= parent
        Frame.__init__(self, self.parent)
        self.pack(expand='yes',fill='both')
        self.label_1= Label(root, text = 'MPD')
        self.label_1.pack(side='right')
        MPDmode = Frame(self)
        MPDmode.pack(side='left')
        self.MPDmode_var = StringVar()
        MPDdigital = Radiobutton(MPDmode, text = 'Digital', variable = self.MPDmode_var,
                                    indicatoron = True, value = 'digital', width = 8,
                                 command= self.callback)
        MPDanalog = Radiobutton(MPDmode, text = 'Analog', variable = self.MPDmode_var,
                                   indicatoron = True, value = 'analog', width = 8,
                                command= self.callback)
        MPDdigital.pack(anchor='center')
        MPDanalog.pack(anchor='center')
        self.MPDmode_var.set('digital')
    def callback(self):
        your_choice= self.MPDmode_var.get()
        print('your choice {}'.format(your_choice))
        
        

if __name__ == '__main__':
    root = Tk()
    MyButton(root)
    root.mainloop()
Reply


Forum Jump:

User Panel Messages

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