Python Single Choice Questions

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python Single Choice Questions


def ask_choice_question(prompt, options):
func = tkinter.Tk()
v = tkinter.IntVar()
v.set(-1)
if(prompt):
tkinter.Label(func, text=prompt).pack()
for i, option in enumerate(options):
tkinter.Radiobutton(func, text=option, variable=v, value=i).pack(anchor="w")
tkinter.Button(func, text="Submit", command=func.destroy).pack()
func.mainloop()
print(v.get())
if(v.get()== -1):
return None
print(options[v.get()])
return options[v.get()]



By throughly looking through this website I have found something like the example above, and modified it slightly.



However, when calling func.destroy it seems the func doesn't return anything, neither do "print()"-s do anything. What do?



EDIT: Actually, something I might add, that this question is asked inside of another Tkinter window, and it actually outputs what needed when that window is closed.



EDIT2: It also may be useful to say, that the menu I'm getting looks something like this: http://prntscr.com/kg516z





When I execute the function, select an answer and click on Submit, both the print and return parts works. Are you sure you had selected an answer before clicking on the button? Because if not, the function returns 'None'.
– j_4321
Aug 8 at 7:51





I am quite sure, yet it actually does return "None" at all times. Also, as stated, the print(v.get()) never actually gets triggered.
– Ludwig Von Chesterfield
Aug 8 at 9:46






Ok, from your edit I think I know what's the problem: you have two Tk instances and your IntVar belongs to the wrong one hence the empty result. I suggest you to use a single Tk instance and replace func = tkinter.Tk() by func = tkinter.Toplevel(). Does it solve your problem?
– j_4321
Aug 8 at 10:36


Tk


IntVar


Tk


func = tkinter.Tk()


func = tkinter.Toplevel()





I can't quite use only one Tk instance to be honest. Any other possible solutions?
– Ludwig Von Chesterfield
Aug 8 at 10:39




1 Answer
1



The issue here is that there are several running Tk instances (i.e. several tcl interpreters) and the IntVar in the function does not belong to func but to the Tk instance created first. As a consequence, its value is not modified by the radiobuttons.


Tk


IntVar


func


Tk



I advise you to use only a single Tk instance, replacing func = tkinter.Tk() by func = tkinter.Toplevel() and using wait_window(func) (wait until func is destroyed) instead of mainloop():


Tk


func = tkinter.Tk()


func = tkinter.Toplevel()


wait_window(func)


func


mainloop()


import tkinter

def ask_choice_question(prompt, options):
func = tkinter.Toplevel(root)
v = tkinter.IntVar(root, -1)
if(prompt):
tkinter.Label(func, text=prompt).pack()
for i, option in enumerate(options):
tkinter.Radiobutton(func, text=option, variable=v, value=i).pack(anchor="w")
tkinter.Button(func, text="Submit", command=func.destroy).pack()
func.wait_window(func)
print(v.get())
if(v.get()== -1):
return None
print(options[v.get()])
return options[v.get()]


root = tkinter.Tk()
tkinter.Button(root, text='Question',
command=lambda: print('returns ', ask_choice_question('Question?', ['Answer %i' % i for i in range(1, 5)]))).pack()
root.mainloop()



If you really want to keep the multiple Tk instances, then you need to specify the master of your IntVar and also to replace mainloop() by wait_window(func) (though I don't know why):


Tk


IntVar


mainloop()


wait_window(func)


import tkinter

def ask_choice_question(prompt, options):
func = tkinter.Tk()
v = tkinter.IntVar(func, -1)
if(prompt):
tkinter.Label(func, text=prompt).pack()
for i, option in enumerate(options):
tkinter.Radiobutton(func, text=option, variable=v, value=i).pack(anchor="w")
tkinter.Button(func, text="Submit", command=func.destroy).pack()
func.wait_window(func)
print(v.get())
if(v.get()== -1):
return None
print(options[v.get()])
return options[v.get()]

root = tkinter.Tk()
tkinter.Button(root, text='Question',
command=lambda: print('returns ', ask_choice_question('Question?', ['Answer %i' % i for i in range(1, 5)]))).pack()
root.mainloop()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard