【问题标题】:Python Tkinter radiobutton program debuggingPython Tkinter 单选按钮程序调试
【发布时间】:2013-12-03 05:51:11
【问题描述】:

本质上,该程序是一组单选按钮,当用户单击提交按钮时,程序应返回与用户所做选择字符串匹配的答案。我已经让它大部分工作了,但最终做出的决定仍然是错误的。让我知道我是否可以回答更多问题。

from Tkinter import *
import tkMessageBox


root = Tk()

q1 = IntVar()

Label(root,
      text="""How many samples do you have?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text="One",
            padx = 20,
            variable=q1,
            value=1).pack(anchor=W)

Radiobutton(root,
            text="Two",
            padx = 20,
            variable=q1,
            value=2).pack(anchor=W)


q2 = IntVar()

Label(root,
      text="""Which choice most closely fits your sample size?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Less than 30""",
            padx = 20,
            variable=q2,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """Greater than or equal to 30""",
            padx = 20,
            variable=q2,
            value = 2).pack(anchor =W)



q3 = IntVar()

Label(root,
      text="""Is the population mean known?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q3,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q3,
            value = 2).pack(anchor=W)


q4 = IntVar()

Label(root,
      text="""Is the standard deviation of your data known?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q4,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q4,
            value = 2).pack(anchor =W)


q5 = IntVar()

Label(root,
      text="""Do you wish to compare two groups?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q5,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q5,
            value = 2).pack(anchor =W)


q6 = IntVar()

Label(root,
      text="""Do you want to compare two sample means?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q6,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q6,
            value = 2).pack(anchor =W)




q7 = IntVar()

Label(root,
      text="""Is your data paired (E.g. before and after data)?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q7,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q7,
            value = 2).pack(anchor =W)



q8 = IntVar()

Label(root,
      text="""Are you testing proportions?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q8,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q8,
            value = 2).pack(anchor =W)



q9 = IntVar()

Label(root,
      text="""Do you wish to test for a difference between observed and expected data?""",
      justify = LEFT,
      padx = 20).pack()

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q9,
            value = 1).pack(anchor=W)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q9,
            value = 2).pack(anchor =W)


Button(root, text = "Submit", command=choose).pack()

def choose():
    if q1 == 1 and q2 == 1 and q3 == 2 and q4 == 2 and  q5 == 1 and q6 == 2 and q7 == 2 and q8 == 2 and q9 == 2 :
        tkMessageBox.showinfo( 'You should use the t-test!')

    elif q1 == 1 and q2 == 2 and q3 == 1 and q4 == 1 and q5 == 2 and q6 == 2 and q7 == 2 and  q8 == 2 and q9 == 2:
        tkMessageBox.showinfo('You should use the z-test!')

    elif q1 == 1 and q2 == 1 and q3 == 2 and q4 == 2 and q5 == 1 and q6 == 2 and q7 == 1 and q8 == 2 and q9 == 2:
        tkMessageBox.showinfo( 'You should use the paired t-test!')

    elif q1 == 2 and q2 == 1 and q3 == 2 and q4 == 2 and q5 == 1 and q6 == 1 and q7 == 2 and q8 == 2 and q9 == 2:
        tkMessageBox.showinfo( 'You should use the two-sample t-test!')

    elif q1 == 2 and q2 == 2 and q3 == 1 and q4 == 1 and q5 == 2 and q6 == 1 and q7 == 2 and q8 == 2 and q9 == 2:
        tkMessageBox.showinfo('You should use the two-sample z-test!')

    elif q1 == 1 and q2 == 2 and q3 == 1 and q4 == 1 and q5 == 2 and q6 == 2 and q7 == 2 and q8 == 1 and q9 == 2:
        tkMessageBox.showinfo('You should use the 1-prop z-test!')

    elif q1 == 2 and q2 == 2 and q3 == 1 and q4 == 1 and q5 == 2 and q6 == 2 and q7 == 2 and q8 == 1 and q9 == 2:
        tkMessageBox.showinfo('You should use the 2-prop z-test!')

    elif q1 == 1 and q2 == 2 and q3 == 2 and q4 == 2 and q5 == 2 and q6 == 2 and q7 == 2 and q8 == 2 and q9 == 1:
        tkMessageBox.showinfo( 'You should use the chi-square test!')

    else:
        tkMessageBox.showinfo('You have either incorrectly answered a question about your data or none of the available tests are appropriate.')
        sys.exit()

root.mainloop()

【问题讨论】:

    标签: python tkinter radio-button


    【解决方案1】:

    在比较 Intvar 实例时,您应该使用 get 方法,并且您的 tkMessageBox.showinfo(title, message) 函数需要两个参数,而不是只传递标题而不传递任何消息,因此将代码更改为如下所示:

    if q1.get() == 1 and q2.get() == 1 and q3.get() == 2 and q4.get() == 2 and  q5.get() == 1 and q6.get() == 2 and q7.get() == 2 and q8.get() == 2 and q9.get() == 2 :
        tkMessageBox.showinfo('Info','You should use the t-test!')
    

    也不要使用sys.exit() 退出你的程序,这会导致你的程序无响应,而是使用root.destroy()

    希望您的消息框现在应该按预期工作!

    可选:使用tkMessageBox.showerror(title, message) 显示错误消息

    【讨论】:

    • 谢谢!我这样做了,我仍然遇到同样的问题,即使我完全遵循 if 语句中的决策路径,它总是会打印出最终的 else 语句。即使我应该得到不同的答案。我的问题可能是什么?
    • @kellie92 更新了答案顺便说一句,我不明白你的意思是你仍然遇到同样的问题......
    【解决方案2】:

    您应该允许未找到的条件。列表也使恕我直言更容易理解。以下内容应该是不言自明的。它只测试前两个条件,第三个选项可以轻松测试代码,并添加了打印语句进行测试。你可以从那里扩展。并且使用列表或函数将大大减少“标签、按钮和按钮”代码的数量。

    from Tkinter import *
    import tkMessageBox
    
    
    def compare_lists(q_list, results_list):
        """ see if the radio buttons are in one of the results
            If so, return the number of the matching list
        """
        for ctr, sub_list in enumerate(results_list):
            if sub_list == q_list:
                print "Found", ctr, sub_list
                return ctr
        return -1
    
    def choose():
        q_list = []
        results_list = [[1, 1, 2, 2, 1, 2, 2, 2, 2],
                        [1, 2, 1, 1, 2, 2, 2, 2, 2],
                        [1, 1, 1, 1 ,1 ,1 ,1 ,1 ,1]]
        print_list = ['You should use the t-test!',
                      'You should use the z-test!',
                      'Testing Only']
        for var in (q1, q2, q3, q4, q5, q6, q7, q8, q9):
            q_list.append(var.get())
        print q_list
    
        ## ctr = number of matching list and will print
        ## the same number in the print list
        ctr = compare_lists(q_list, results_list)
        if ctr > -1:
            result_label.set(print_list[ctr])
        else:
            result_label.set('Combination not found')
    
    root = Tk()
    
    q1 = IntVar()
    
    Label(root,
          text="""How many samples do you have?""",
          justify = LEFT,
          padx = 20).pack()
    
    Radiobutton(root,
            text="One",
            padx = 20,
            variable=q1,
            value=1).pack(anchor=W)
    
    Radiobutton(root,
            text="Two",
            padx = 20,
            variable=q1,
            value=2).pack(anchor=W)
    
    
    q2 = IntVar()
    
    Label(root,
      text="""Which choice most closely fits your sample size?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Less than 30""",
            padx = 20,
            variable=q2,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """Greater than or equal to 30""",
            padx = 20,
            variable=q2,
            value = 2).pack(anchor =W)
    
    
    
    q3 = IntVar()
    
    Label(root,
      text="""Is the population mean known?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q3,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q3,
            value = 2).pack(anchor=W)
    
    
    q4 = IntVar()
    
    Label(root,
      text="""Is the standard deviation of your data known?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q4,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q4,
            value = 2).pack(anchor =W)
    
    
    q5 = IntVar()
    
    Label(root,
      text="""Do you wish to compare two groups?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q5,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q5,
            value = 2).pack(anchor =W)
    
    
    q6 = IntVar()
    
    Label(root,
      text="""Do you want to compare two sample means?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q6,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q6,
            value = 2).pack(anchor =W)
    
    
    
    
    q7 = IntVar()
    
    Label(root,
      text="""Is your data paired (E.g. before and after data)?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q7,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q7,
            value = 2).pack(anchor =W)
    
    
    
    q8 = IntVar()
    
    Label(root,
      text="""Are you testing proportions?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q8,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q8,
            value = 2).pack(anchor =W)
    
    
    
    q9 = IntVar()
    
    Label(root,
      text="""Do you wish to test for a difference between observed and expected data?""",
      justify = LEFT,
      padx = 20).pack()
    
    Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q9,
            value = 1).pack(anchor=W)
    
    Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q9,
            value = 2).pack(anchor =W)
    
    
    result_label = StringVar()
    result_label.set("")
    Label(root, textvariable=result_label, fg="red").pack()
    Button(root, text = "Submit", command=choose, fg="blue", bg="white").pack()
    Button(root, text = "EXIT", command=root.quit,
           bg="blue", fg="yellow").pack()
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 2016-09-01
      • 2016-06-10
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多