如何使单选按钮为变量指定0、1、2或3?

2024-05-16 17:54:37 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我做的一个选择题的粗略例子。它在终端上运行,我希望用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()

我已经工作了两天了,教自己如何使用Tkinter。我和我的朋友做了一个粗略的测试示例。

^{pr2}$

使用这个Tkinter GUI,我可以想到几个目标:

  • 使单选按钮将0、1、2或3分配给我的变量(BAI_var1、BAI_var2、BAI_3等)
  • 使“下一步”和“上一步”按钮每次按时都显示不同的问题。在
  • 在测试结束时创建一个页面,其中包含名称、日期等的多个输入字段

我没有参加考试!!我只做了软件。这个测试是由davidburns设计的,可以在他的著作《感觉良好的书》中找到。在


Tags: orforinputnotallatfilewrite
2条回答

self.v更改v的所有位置,使其成为对象的属性。然后您将看到它具有在单选按钮组中选择的任何值。在

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

一旦您这样做了,在您的button5button6方法中,您可以执行print self.v.get()来查看值。在

如果您现在对学习tkinter不感兴趣,easygui将非常适合于快速获得此测试的GUI。它不在标准库中,因此您必须使用pip install安装它,或者下载它并将轻松.py与脚本位于同一文件夹中。Easygui允许您执行以下操作:

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

相关问题 更多 >