Basic Python Tutorial 40 – Radio Button Program

///Basic Python Tutorial 40 – Radio Button Program

Basic Python Tutorial 40 – Radio Button Program

FavoriteLoadingAdd to favorites

In this tutorial we will be learning to use Radio buttons. It is the same as check boxes basically. Please view the follow up video that I have to find out what mistake that I made. Sorry about that!

Investary Website —

Twitter –

Facebook –
Questions, comments, feedback? Leave it in the comments section.
Also if you can please subscribe and like this video, it will help me tremendously.

source

By |2020-05-15T03:36:51+00:00May 15th, 2020|Python Video Tutorials|13 Comments

13 Comments

  1. Diomedes Hernandez May 15, 2020 at 3:36 am - Reply

    Set a value to the radio button

  2. Senchi May 15, 2020 at 3:36 am - Reply

    This is Python 2 it does not work in Python 3 🙁

  3. Mohit Malik May 15, 2020 at 3:36 am - Reply

    Here is the complete working example:

    import Tkinter

    class Application(Frame):

    def _init_(self,master):
    Frame.__init__(self,master)
    self.grid()
    self.button_clicks = 0
    self.create_widgets()

    def create_widgets(self):
    Label(self,text = "choose your text"
    ).grid(row = 0, column = 1, sticky = W)

    Label(self,text = "Select All that apply"
    ).grid(row = 1, column = 1, sticky = W)

    self.favorite = StringVar()
    self.favorite.set("1")

    Radiobutton(self,
    text="Comedy",
    variable = self.favorite,
    value = "Commedy",
    command = self.update_text
    ).grid(row=2, column=0, sticky=W)
    Radiobutton(self,
    text="Drama",
    variable = self.favorite,
    value = "Drama",
    command = self.update_text
    ).grid(row=3, column=0, sticky=W)
    Radiobutton(self,
    text="Romance",
    variable = self.favorite,
    value = "Romance",
    command = self.update_text
    ).grid(row=4, column=0, sticky=W)

    self.result = Text(self,width = 40, height=5, wrap= WORD)
    self.result.grid(row=5, column=0, columnspan=3)

    def update_text(self):
    message = "your fav movie "
    message += self.favorite.get()

    self.result.delete(0.0, END)
    self.result.insert(0.0, message)

    root = Tk()
    root.title("Labeler")
    root.geometry("300×300")

    app = Application(root)
    root.mainloop()

  4. Diógenes José May 15, 2020 at 3:36 am - Reply

    only put the "value" parameter after "command", for instance, " command = self.update_text, value = 1,"…

  5. Marcin K May 15, 2020 at 3:36 am - Reply

    Each instantiation of Radiobutton needs a value=… parameter, so that it know what value to use to assign to the 'variable' you pointed it to. Following code works:

    Radiobutton(self,
    text="Comedy",
    variable = self.favorite,
    value = "Commedy",
    command = self.update_text
    ).grid(row=2, column=0, sticky=W)
    Radiobutton(self,
    text="Drama",
    variable = self.favorite,
    value = "Drama",
    command = self.update_text
    ).grid(row=3, column=0, sticky=W)
    Radiobutton(self,
    text="Romance",
    variable = self.favorite,
    value = "Romance",
    command = self.update_text
    ).grid(row=4, column=0, sticky=W)

    This will start without a default value. To start with a default value add following to end of create_widgets()

    self.favourite.set("Commedy")
    self.update_text()

  6. Skilbhumen May 15, 2020 at 3:36 am - Reply

    you are god for me i never forgot you
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  7. ShadowRex Gaming May 15, 2020 at 3:36 am - Reply

    very much thank you sir , you are my great tutor! and i thinking if i wanna assign some values on radio button like if we click button type 2 , the price for the option will be 70 dollar. i just create the var = IntVar() for that and set() the value and later on call inside the update_text method. i was probably stuck for an hour searching for the problem, the only thing i left was convert syntax from integer to string, str(self.price.get()) . hahaha. 😀 

  8. Maciej K. May 15, 2020 at 3:36 am - Reply

    You can add: self.favorite.set("1") below: self.favorite = StringVar() if you dont want all the buttons to be automatically selected. Thank you for the tutorial!

  9. Swolyfield May 15, 2020 at 3:36 am - Reply

    Hi,
    I am not sure whether or not you are still responding to questions, but I will give it a shot. I have written a program that uses radiobuttons to show a list of strings, and then there is a button which picks a random string from that list of strings. The problem is that the radiobutton only accepts Intvar and stringVar so I have to convert the "list" of strings (one big string) into a list by using xxxx.split(','). Is there any way to assign a list to a radiobutton?
    Thanks in advance,
    Swolyfield

  10. ali irsha May 15, 2020 at 3:36 am - Reply

    We have to add the value parameter.

    value = "Drama"

  11. Mahmud Sumon May 15, 2020 at 3:36 am - Reply

    Hi investary,
    I learn a lot from your python tutorial.
    Can you do a tutorial of 2nd/3rd depth (level) of web crawling.
    Thank you so much for your video.

  12. Youtro May 15, 2020 at 3:36 am - Reply

    When is that? 😛

  13. Dr Sunil Patil May 15, 2020 at 3:36 am - Reply

    the problem is with the variable names. Use different variable names for the three radio buttons. This will solve the problem. (variable=self.favorite, is common to all, which should be different)

Leave A Comment

*