简体   繁体   English

Python Tkinter单选按钮程序调试

[英]Python Tkinter radiobutton program debugging

Essentially the program is a group of radio buttons and when the user clicks the submit button, the program should return the answer matching the string of choices made by the user.本质上,该程序是一组单选按钮,当用户单击提交按钮时,该程序应返回与用户所做的选择字符串匹配的答案。 I've gotten it to work for the most part, but the decision made in the end is still wrong.我已经让它在很大程度上起作用了,但最终做出的决定仍然是错误的。 Let me know if I can answer anymore questions.让我知道我是否可以回答更多问题。

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()

You should use the get method when comparing Intvar instances and you need two arguments for your tkMessageBox.showinfo(title, message) function instead your passing only the title with no message so change your code to look like:您应该在比较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!')

and also don't use sys.exit() to exit you program it will cause your program to be unresponsive instead use root.destroy()并且也不要使用sys.exit()退出你的程序它会导致你的程序没有响应而是使用root.destroy()

Hopefully your message boxes should now be working as expected!希望您的消息框现在应该按预期工作!

在此处输入图片说明

optional: use tkMessageBox.showerror(title, message) to display error messages可选:使用tkMessageBox.showerror(title, message)显示错误消息

You should allow for not found conditions.您应该允许未找到的条件。 Also lists make it easier to understand IMHO.列表还可以更容易地理解恕我直言。 The following should be self-explanatory.以下内容应该是不言自明的。 It only tests for the first 2 conditions with a third option for an easy test of the code, with print statements added for testing.它只测试前两个条件,第三个选项可以轻松测试代码,并添加了打印语句进行测试。 You can expand from there.你可以从那里扩展。 Also the use of lists or a function will greatly reduce the amount of "Label, Button, and Button" code.此外,使用列表或函数将大大减少“标签、按钮和按钮”代码的数量。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM