How to get the value of the selected radio button?

Zoxx

I would like to create 2 different groups of radio buttons. The user would select one option from either group. There would be a function that would get the values(strings) from the selected radio buttons and then print them. Here's my code but it doesn't work (i'm new to python).

from tkinter import *
root = Tk()
btn1 = "lol"
btn2 = "lel"
def funkcija():
    n = entry1.get()
    m = "null"
    X = btn1.get()
    Y = btn2.get()
    print("%s %s je %s %s." % (n, X, m, Y))


theLabel = Label(root, text="Vnesite količino in izberite prvo valuto.")
theLabel.grid(row=0, columnspan=3)

gumb1=Radiobutton(root,text="Euro",value = "euro",variable = "btn1").grid(row=2, column=1, sticky=W)
gumb2=Radiobutton(root,text="Dolar",value = "dolar",variable = "btn1").grid(row=3, column=1, sticky=W)
gumb3=Radiobutton(root,text="Funt",value = "funt",variable = "btn1").grid(row=4, column=1, sticky=W)

label3= Label(root, text="Izberite drugo valuto.")
label3.grid(row=6, columnspan=3)

label35= Label(root)
label35.grid(row=5, columnspan=3)

gumb4=Radiobutton(root,text="Euro",value = "euro",variable = "btn2").grid(row=7, column=1, sticky=W)
gumb5=Radiobutton(root,text="Dolar",value = "dolar",variable = "btn2").grid(row=8, column=1, sticky=W)
gumb6=Radiobutton(root,text="Funt",value = "funt",variable = "btn2").grid(row=9, column=1, sticky=W)

label1 = Label(root, text="Količina:")
label1.grid(row=1, sticky=E)
entry1 = Entry(root)
entry1.grid(row=1, column=1, sticky=W)

go = Button(root, text="Izračun", fg="white", bg="black", command=funkcija)
go.grid(row=10, columnspan=3)

root.mainloop()
Zizouz212

In your radio button, analyze the parameters that you are passing:

gumb1 = Radiobutton(root,
                    text = "Euro",
                    value = "Euro",
                    variable = "btn2"

The parameters value and variable are what stores the data of the radio button. You've set your value option correctly. The interpreter will automatically set the variable with the value when the radio button is selected.

But here's where your issue is:

variable = "btn2"

"btn2" is a string. Not very useful though, is it? In fact, you're trying to perform methods on it that don't even exist. Such as here:

def funkcija():
    X = btn2.get()

In fact, taking this information, you almost got there!

At the top of your script, you need to set btn2 to Tkinter's StringVar, like so:

from tkinter import *
btn1 = StringVar()
btn2 = StringVar()

Now that's done, let's change our parameters in our radio buttons.

gumb1 = Radiobutton(root,
                text = "Euro",
                value = "Euro",
                variable = btn2

Now, Tkinter will automatically update the variable when it is selected. To get the value, do the same that you had done in your funkcija.

X = btn2.get()

And then the value of btn2 (which was updated by the radio buttons) will not be read, and stored into the variable X.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get value of selected radio button?

From Dev

How to get value of selected radio button in angularjs

From Dev

How To get Selected Radio Button Value in Mvc

From Dev

How get value of selected radio button in datalist

From Dev

How to get the NAME and value of a radio button when DYNAMICALLY selected - in jQuery?

From Dev

How to get selected value of list item in radio button list in jquery

From Dev

How to get selected radio button value onclick from php

From Dev

How to get the selected radio button value inside the form from expressjs?

From Dev

How to get the NAME and value of a radio button when DYNAMICALLY selected - in jQuery?

From Dev

How do i Get value of selected radio button in angularjs?

From Dev

get hidden value of the currently selected radio button

From Dev

get hidden value of the currently selected radio button

From Dev

Get selected radio button value in javascript

From Dev

Get value of selected radio-button?

From Dev

How to get selected radio button from ToggleGroup

From Dev

How to get selected radio button values?

From Dev

How to get the pk of the selected radio button in django

From Dev

How to get last selected radio button id

From Dev

How to get the selected value from a radio button group in React where there are multiple groups?

From Dev

How to get whole row value if radio button on one cell is selected in php

From Dev

How can I get the selected radio button value on another jsp page?

From Dev

How do I get the value of a user-selected radio button in the controller?

From Dev

How to get the value of a selected radio button and checkbox using its class in jQuery?

From Dev

how to get selected value from radio button from html in angular Js

From Dev

Bootstrap radio button: Get selected value on submit form

From Dev

Trying to get value of selected radio button using ajax/jquery

From Dev

Get selected button's value from Polymer radio group with jquery

From Dev

Get selected radio button value in Ajax-generated tooltip

From Dev

Getting radio button selected value

Related Related

  1. 1

    How to get value of selected radio button?

  2. 2

    How to get value of selected radio button in angularjs

  3. 3

    How To get Selected Radio Button Value in Mvc

  4. 4

    How get value of selected radio button in datalist

  5. 5

    How to get the NAME and value of a radio button when DYNAMICALLY selected - in jQuery?

  6. 6

    How to get selected value of list item in radio button list in jquery

  7. 7

    How to get selected radio button value onclick from php

  8. 8

    How to get the selected radio button value inside the form from expressjs?

  9. 9

    How to get the NAME and value of a radio button when DYNAMICALLY selected - in jQuery?

  10. 10

    How do i Get value of selected radio button in angularjs?

  11. 11

    get hidden value of the currently selected radio button

  12. 12

    get hidden value of the currently selected radio button

  13. 13

    Get selected radio button value in javascript

  14. 14

    Get value of selected radio-button?

  15. 15

    How to get selected radio button from ToggleGroup

  16. 16

    How to get selected radio button values?

  17. 17

    How to get the pk of the selected radio button in django

  18. 18

    How to get last selected radio button id

  19. 19

    How to get the selected value from a radio button group in React where there are multiple groups?

  20. 20

    How to get whole row value if radio button on one cell is selected in php

  21. 21

    How can I get the selected radio button value on another jsp page?

  22. 22

    How do I get the value of a user-selected radio button in the controller?

  23. 23

    How to get the value of a selected radio button and checkbox using its class in jQuery?

  24. 24

    how to get selected value from radio button from html in angular Js

  25. 25

    Bootstrap radio button: Get selected value on submit form

  26. 26

    Trying to get value of selected radio button using ajax/jquery

  27. 27

    Get selected button's value from Polymer radio group with jquery

  28. 28

    Get selected radio button value in Ajax-generated tooltip

  29. 29

    Getting radio button selected value

HotTag

Archive