开发者

How do I make radiobuttons assign either a 0,1,2 or 3 to my variables?

开发者 https://www.devze.com 2023-04-13 05:24 出处:网络
Here is a rough example of a multiple choice test that I made. It runs in terminal and I\'m hoping to make a Graphical User Interface for it using Tkinter.

Here is a rough example of a multiple choice test that I made. It runs in terminal and I'm hoping to make a Graphical User Interface for it using Tkinter.

print "CATEGORY 1: ANXIOUS FEELINGS"
print
print "1. Anxiety, nervousness, worry or fear"
BAI_var1 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")
print
print "2. Feeling that things around you are strange, unreal, or foggy"
BAI_var2 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")
print
print "3. Feeling detached from all or part of your body"
BAI_var3 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")

print "Depression Test"
print
print
print "1. Sadness: Have been feeling sad or down in the dumps?"
BDC_var1 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")
print
print "2. Discouragement: Does the future look hopeless?"
BDC_var2 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")
print
print "3. Low self-esteem: Do you feel worthless or think of yourself as a failure?"
BDC_var3 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")

#Burns Anxiety Inventory
#CATEGORY 1: ANXIOUS FEELINGS
Cat1_var = BAI_var1 + BAI_var2 + BAI_var3

#Burns Anxiety Checklist
BAI_var = Cat1_var
#Burns Depression Checklist
BDC_var = BDC_var1 + BDC_var2 + BDC_var3

#Prints your BAI & your BDC
print "Your BAI =", BAI_var,"Your BDC =", BDC_var


name = raw_input ("Please type in your name:")
bai = str(input("Please enter your BAI:"))
bdc = str(input("Please enter your BDC:"))
year = str(input("please enter the year:"))
month = str(input("Please enter the month:"))
day = str(input("Please enter day:"))
time_hour = str(input("Please enter the current hour:"))
time_minute = str(input("Please enter the current minutes:"))
am_pm = raw_input ("Please enter pm or am:")

file = open('Burns inventory 1.3.txt', 'a')
file.write(name + '\n')
file.write(day)
file.write("/")
file.write(month)
file.write("/")
file.write(year)
file.write('\n')
file.write('\n')
file.write('Your BAI is:')
file.write(bai)
file.write('\n')
file.write('Your BDC is:')
file.write(bdc)
file.write('\n')
file.write(time_hour)
file.write(':')
file.write(time_minute)
file.write('\n')
file.write(' ')
file.write(am_pm)
file.write('\n')
file.close()

I have been working for 2 days two teach myself how to use Tkinter. My friend and I made this rough example of what the test might look like.

from Tkinter import *
import time

class App:
    def __init__(self, master):
        w = Label(master, text="1. Anxiety, nervousness, worry or fear")
        w.pack()

        v = IntVar()
        Radiobutton(master, text="0 for not at all", variable=v, value=1).pack(side=TOP, anchor="w")
        Radiobutton(master, text="1 for somewhat", variable=v, value=2).pack(side=TOP, anchor="w")
        Radiobutton(master, text="2 for moderatly", variable=v, value=3).pack(side=TOP, anchor="w")
        Radiobutton(master, text="3 for a lot", variable=v, value=4).pack(side=TOP, anchor="w"开发者_StackOverflow)

        self.button = Button(master, text="BACK", fg="red", command=self.button6)
        self.button.pack(side=BOTTOM)
        self.button = Button(master, text="NEXT", fg="red", command=self.button5)
        self.button.pack(side=BOTTOM)


    def button6(self):
        print "Sam is awesome!GAJONGA" 

    def button5(self):
        print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY" 

master = Tk()
app = App(master)
master.mainloop()

I can think of a few goals with this Tkinter GUI:

  • Make Radiobuttons assign either a 0,1,2 or 3 to my variables(BAI_var1, BAI_var2, BAI_3 etc.)
  • Make the "NEXT" & "BACK" buttons show a different question every time you press it.
  • Make a page at the end of the test that has multiple input fields for name, date, etc.

I did not make the test!! I only made the software. The test was designed by David Burns and is available in his work book "The Feeling Good Book".


change all of the places where you have v with self.v so that it becomes an attribute of the object. You'll then see that it has whatever value is selected in the group of radiobuttons.

...
self.v = IntVar()
...
Radiobutton(master, text="1 for somewhat", variable=self.v, value=2)...
...

Once you do that, in your button5 or button6 method you can do print self.v.get() to see the value.


If you're not interested in learning tkinter right now, easygui would be a good fit for quickly getting a GUI for this test. It's not in the standard library, so you'll have to either install it with pip install or download it and put the easygui.py file in the same folder as your script. Easygui lets you do things like:

import easygui
animal = easygui.choicebox(
        msg='Pick an animal.',
        title='answer the question',
        choices=('dog', 'cat', 'pig')
        )
print animal
# prints dog or pig or cat
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号