0

Ok guys, I am not able to make those buttons do as they are told. In stead, even before selected, they all just print the same value for the guess variable, if I set it to None, that is printed four times....and I dont know what to do any more. This is the code:

from tkinter import*
root = Tk()
root.title("KVIZOMANIJA")
root.geometry("800x600")
app = Frame(root)
app.grid()
guess = StringVar()
guess.set("opa")
odg1 = Radiobutton(app,text = "prvi gumbek", variable = guess, value = "a", command =print(guess.get()))
odg1.grid(row=4, column = 0, columnspan= 2, sticky = W)
odg2 = Radiobutton(app,text="drugi gumbek", variable = guess, value = "b", command = print(guess.get()))
odg2.grid(row=5, column = 0, columnspan= 2, sticky = W)
odg3 = Radiobutton(app,text="treci gumbek", variable = guess, value = "c", command = print(guess.get()))
odg3.grid(row=6, column = 0, columnspan= 2, sticky = W)
odg4 = Radiobutton(app,text="cetvrti gumbek", variable = guess, value = "d", command = print(guess.get()))
odg4.grid(row=7, column = 0, columnspan= 2, sticky = W)
root.mainloop()

So, this is it....

rdas
  • 20,604
  • 6
  • 33
  • 46

1 Answers1

2

I probably see your problem: it's because you do not set a function in "command" argument, you set an instruction. You should replace

command =print(guess.get())

by

command=lambda x: print(guess.get())

lambda is a way to make an anonymous function with only one instruction in it and it returns the result of the instruction.

You can also define a normal function like this:

def get_guess():
    print(guess.get())

and

from tkinter import*
root = Tk()
root.title("KVIZOMANIJA")
root.geometry("800x600")
app = Frame(root)
app.grid()
guess = StringVar()
guess.set("opa")
odg1 = Radiobutton(app,text = "prvi gumbek", variable = guess, value = "a", command=get_guess)
odg1.grid(row=4, column = 0, columnspan= 2, sticky = W)
odg2 = Radiobutton(app,text="drugi gumbek", variable = guess, value = "b", command=get_guess)
odg2.grid(row=5, column = 0, columnspan= 2, sticky = W)
odg3 = Radiobutton(app,text="treci gumbek", variable = guess, value = "c", command=get_guess)
odg3.grid(row=6, column = 0, columnspan= 2, sticky = W)
odg4 = Radiobutton(app,text="cetvrti gumbek", variable = guess, value = "d", command=get_guess)
odg4.grid(row=7, column = 0, columnspan= 2, sticky = W)
root.mainloop()
clemsciences
  • 121
  • 9