I posted an earlier question and since then I have added user interface to my program and I got stuck.
What I'm trying to do is ask a series of questions with radio buttons "Yes and "No" as the answer with a submit at the bottom of it but I have no idea how to change boolean value inside a certain question
For example:
are you hungry? (radiobutton yes) (radiobutton no)
do you want to order? (radiobutton yes) (radiobutton no)
do you want to cook? (radiobutton yes) (radiobutton no)
(submit)
And then I have set up boolean values for each question but I don't know how to change it to True
with the yes answer and false to the no answer. Here is this part of the code from my program.
from tkinter import *
import random
Are_You_Hungry = False
Do_You_Have_Money_To_Order = False
Do_You_Have_Energy_To_Cook = False
Are_You_In_The_Mood_To_Cook = False
Does_the_Oven_Work = False
Do_You_Want_To_Eat_Warm_Food = False
def Random_dinner_button():
new_window = Tk()
label = Label(new_window,
text="Are You Hungry?: ",
font=("Times New Roman", 10),
padx=10, pady=10)
label.grid(row=0,column=0,columnspan=3,sticky=W)
var = IntVar()
R1 = Radiobutton(new_window,
text="Yes",
variable=var,
value=1,
command=Are_You_Hungry1
)
R1.grid(row=0,column=4)
R2 = Radiobutton(new_window,
text="No",
variable=var,
value=2,
command=Are_You_Hungry1
)
R2.grid(row=0,column=5)
label = Label(new_window,
text="Do You Have Money To Order?: ",
font=("Times New Roman", 10),
padx=10, pady=10)
label.grid(row=1, column=0, columnspan=3,sticky=W)
var = IntVar()
R1 = Radiobutton(new_window,
text="Yes",
variable=var,
value=1,
# command=sel)
)
R1.grid(row=1, column=4)
R2 = Radiobutton(new_window,
text="No",
variable=var,
value=2,
# command=sel)
)
R2.grid(row=1, column=5)
label = Label(new_window,
text="Do You Have Energy To Cook?: ",
font=("Times New Roman", 10),
padx=10, pady=10)
label.grid(row=2, column=0, columnspan=3,sticky=W)
var = IntVar()
R1 = Radiobutton(new_window,
text="Yes",
variable=var,
value=1,
# command=sel)
)
R1.grid(row=2, column=4)
R2 = Radiobutton(new_window,
text="No",
variable=var,
value=2,
# command=sel)
)
R2.grid(row=2, column=5)
label = Label(new_window,
text="Are You In The Mood To Cook?: ",
font=("Times New Roman", 10),
padx=10, pady=10)
label.grid(row=3, column=0, columnspan=3,sticky=W)
var = IntVar()
R1 = Radiobutton(new_window,
text="Yes",
variable=var,
value=1,
# command=sel)
)
R1.grid(row=3, column=4)
R2 = Radiobutton(new_window,
text="No",
variable=var,
value=2,
# command=sel)
)
R2.grid(row=3, column=5)
label = Label(new_window,
text="Does the Oven Work?: ",
font=("Times New Roman", 10),
padx=10, pady=10)
label.grid(row=4, column=0, columnspan=3,sticky=W)
var = IntVar()
R1 = Radiobutton(new_window,
text="Yes",
variable=var,
value=1,
# command=sel)
)
R1.grid(row=4, column=4)
R2 = Radiobutton(new_window,
text="No",
variable=var,
value=2,
# command=sel)
)
R2.grid(row=4, column=5)
label = Label(new_window,
text="Do You Want To Eat Warm Food?: ",
font=("Times New Roman", 10),
padx=10, pady=10)
label.grid(row=5, column=0, columnspan=3,sticky=W)
var = IntVar()
R1 = Radiobutton(new_window,
text="Yes",
variable=var,
value=1,
# command=sel)
)
R1.grid(row=5, column=4)
R2 = Radiobutton(new_window,
text="No",
variable=var,
value=2,
# command=sel)
)
R2.grid(row=5, column=5)
button = Button(new_window,
text="Submit",
font=("Comic Sans", 11, "bold"),
state=ACTIVE)
button.grid(row=6,column=2)
Sorry for the repetitive code, I'm still learning how to code and how to manage it.
CodePudding user response:
You can do it like this (more explanation is in code comments): have a dictionary (no need to clutter your namespace with a bunch of variable names and the dictionary makes it easier to interact with all of the values in it as well as it is easier to save to a file) that contains the answers to the questions, create the widgets in a loop based on the items in the dictionary (removed repeatedness) and set the value of radiobuttons to what is in the dictionary (meaning you can change the default value). Append the variables containing the value to a list along with the question. Have a button that calls a function that will get the current values of the radiobuttons and set those values in the dictionary. Then print the dictionary (which will then contain the new values).
import tkinter as tk
# use a dictionary for easier question and answer management
questions_and_answers = {
'Are you hungry?': False,
'Do you have money to order?': False,
'Do you have energy to cook?': False,
'Are you in the mood to cook?': False,
'Does the oven work?': False,
'Do you want to eat warm food?': False
}
# create a list for storing question text (key to the above dictionary)
# and variable that contains the radiobutton value
user_answers = []
def submit():
# go over the question and variable in the user_answers dictionary
for q, a in user_answers:
# set the question value to the value selected by the user
questions_and_answers[q] = a.get()
# print what the main question and answer dictionary contains
print(questions_and_answers)
root = tk.Tk()
# labels for telling user which row is for what
tk.Label(root, text='Yes').grid(row=0, column=1, sticky='news')
tk.Label(root, text='No').grid(row=0, column=2, sticky='news')
# go over the main question dictionary and start enumerating from one since the
# first row is for the above labels
for row, (question, answer) in enumerate(questions_and_answers.items(), start=1):
# create the label to contain the question
lbl = tk.Label(root, text=question)
# variable for getting selected radiobutton
var = tk.BooleanVar(value=answer)
# radiobuttons to select yes or no
yes = tk.Radiobutton(root, variable=var, value=True)
no = tk.Radiobutton(root, variable=var, value=False)
# append the question and variable to the user_answers list
user_answers.append((question, var))
# place the label and radiobuttons
lbl.grid(row=row, column=0, sticky='news')
yes.grid(row=row, column=1, sticky='news')
no.grid(row=row, column=2, sticky='news')
# create the submit button which when clicked will call the `submit` function
btn = tk.Button(root, text='Submit', command=submit)
# grid the button on the last row and expand it over the 3 columns
btn.grid(row=root.grid_size()[1], column=0, columnspan=3, sticky='news')
root.mainloop()
Also:
I strongly advise against using wildcard (*
) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2
and so on or import the whole module: import module
then You can also use an alias: import module as md
or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.
I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case
, class names in CapitalCase
. Don't have space around =
if it is used as a part of keyword argument (func(arg='value')
) but have space around =
if it is used for assigning a value (variable = 'some value'
). Have space around operators ( -/
etc.: value = x y
(except here value = x y
)). Have two blank lines around function and class declarations. Object method definitions have one blank line around them.
CodePudding user response:
I would suggest storing the text of the questions and their associated answers in dictionary because that will make it easy to processing them as a group and get rid of a lot that repetitious code you have in your question. I would also strong suggest that your read and start following the PEP 8 - Style Guide for Python Code guidelines which will make your code more readable.
Here's the results of doing that way based on what you have in your question. Note how much shorter and relatively easy to read it has become.
mport tkinter as tk
root = tk.Tk()
questions = ("Are You Hungry?",
"Do You Have Money To Order?",
"Do You Have Energy To Cook?",
"Are You In The Mood To Cook?",
"Does the Oven Work?",
"Do You Want To Eat Warm Food?")
answers = {question: tk.BooleanVar(value=False) for question in questions}
def yn_question(win, row, var, text):
label = tk.Label(win, text=text " ", font=("Times New Roman", 10), padx=10, pady=10)
label.grid(row=row, column=0, columnspan=3, sticky="W")
yes_btn = tk.Radiobutton(win, text="Yes", variable=var, value=True)
yes_btn.grid(row=row, column=4)
no_btn = tk.Radiobutton(win, text="No", variable=var, value=False)
no_btn.grid(row=row, column=5)
def submit():
# Show answers,
for question, answer in answers.items():
print(f'{question} {answer.get()}')
def Random_dinner_button(window):
for row, (question, var) in enumerate(answers.items()):
yn_question(window, row, var, question)
button = tk.Button(window, text="Submit", font=("Comic Sans", 11, "bold"),
command=submit)
button.grid(row=6,column=2)
Random_dinner_button(root)
root.mainloop()