Python Tkinter文件对话框

Python Tkinter文件对话框,python,file-upload,tkinter,filedialog,Python,File Upload,Tkinter,Filedialog,有没有可能允许程序用户在收到程序关于他们应该使用哪个测试的答案后上传他们的数据?我知道我需要使用tkFileDialog,但在程序的第一部分运行之后,我在运行它时遇到了问题。如果不清楚,请告诉我。到目前为止,我的代码是: from Tkinter import * import tkMessageBox root = Tk() q1 = IntVar() Label(root, text="""How many samples do you have?""",

有没有可能允许程序用户在收到程序关于他们应该使用哪个测试的答案后上传他们的数据?我知道我需要使用tkFileDialog,但在程序的第一部分运行之后,我在运行它时遇到了问题。如果不清楚,请告诉我。到目前为止,我的代码是:

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.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( 'decision', 'You should use the t-test!')

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

        elif 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() == 1 and q8.get() == 2 and q9.get() == 2:
            tkMessageBox.showinfo('decision', 'You should use the paired t-test!')

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

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

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

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

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

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



root.mainloop()   

如果您需要打开的所有文件都可以从中心位置访问,您可以让
If
语句打开他们需要的文件

尝试将这些文件设置为变量:

t\u test=r'Path\To\File\location\t-test.File'

等等,你剩下的文件

然后,与其告诉他们打开什么,不如用
os.system()
打开它:

os.system(t\u测试)


别忘了将操作系统导入到你的其他导入中。

当然,在互联网的帮助下,我自己解决了这个问题。我的更新代码是:

from Tkinter import *
import tkMessageBox
from scipy import stats
import csv


sample=[]


def load_file():

    fname = askopenfilename(filetypes = (("Text Files", ".txt"),
                            ("HTML Files", "*.html;*.htm"),
                                ("All Files", "*.*")))
    global sample
    if fname:
        print "uploading file...",fname
        try:
            print("""Here comes the file""")
        except:
            showerror("Open Source File", "Failed to read file\n'%s'" % fname)
        ifile  = open(fname, "rb")

        data = csv.reader(ifile,delimiter=' ')

        rownum = 0

        for row in data:
            colnum = 0
            d=[]
            for col in row:
                print '%s' % col
                d.append(float(col))
                colnum += 1
            sample.append(d)
            rownum += 1

        ifile.close()
        print sample


def choose():

        global sample
        if q1.get() == 1 and q2.get() == 1 and q3.get() == 1 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('t-test', 't-value is %s, p=%s' %stats.ttest_1samp(sample[0], 0.50 ))
            #will do t-test here
            print sample
            # now sample list contains the data for doing t-test,
            #you just need to call t-test to get the results and show it on message box


        elif q1.get() == 1 and q2.get() == 2 and q3.get() == 1 and q4.get() == 1 and q5.get() == 2 and q6.get() == 2 and q7.get() == 2 and  q8.get() == 2 and q9.get() == 2:
             tkMessageBox.showinfo('z-test', 'test statistic is %s, p=%s'%stats.ttest_1samp(sample[0], 5.0))

        elif 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() == 1 and q8.get() == 2 and q9.get() == 2:
            tkMessageBox.showinfo('Paired t-test', 'test statistic is %s, p=%s'%stats.ttest_rel(sample[0], sample[1]))

        elif q1.get() == 2 and q2.get() == 1 and q3.get() == 2 and q4.get() == 2 and q5.get() == 1 and q6.get() == 1 and q7.get() == 2 and q8.get() == 2 and q9.get() == 2:
             tkMessageBox.showinfo('Two sample t-test', 'test statistic is %s, p=%s'%stats.ttest_ind(sample[0], sample[1]))

        elif q1.get() == 2 and q2.get() == 2 and q3.get() == 1 and q4.get() == 1 and q5.get() == 2 and q6.get() == 1 and q7.get() == 2 and q8.get() == 2 and q9.get() == 2:
             tkMessageBox.showinfo('Two sample z-test', 'test statistic is %s, p=%s'%stats.ttest_ind(sample[0], sample[1]))



        else:
            tkMessageBox.showinfo('decision', 'You have either incorrectly answered a question about your data or none of the available tests are appropriate.')
            #root.destroy()
root = Tk()

q1 = IntVar()

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

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


q2 = IntVar()

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

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

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



q3 = IntVar()

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

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

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


q4 = IntVar()

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

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

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


q5 = IntVar()

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

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

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


q6 = IntVar()

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

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

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




q7 = IntVar()

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

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

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



q8 = IntVar()

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

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

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



q9 = IntVar()

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

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

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




from tkFileDialog import askopenfilename
from tkMessageBox import showerror

Button(root,text = "Browse files", command = load_file, width = 10).pack()



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




root.mainloop()   

定义“上传”。用户从“文件”对话框中选择文件后,数据会发生什么变化?长期目标是使用他们选择的文件,以便运行他们被告知最适合其数据的统计程序。现在,我需要允许他们选择他们想要使用的文件。在回答所有问题后,你想打开2-prop测试吗?如果是这样,这些文件是否存储在每个人都可以访问的位置(例如服务器或每个人都使用一台计算机)?我下面的回答是否解决了您的问题?不完全是,这是我的错,因为我的问题没有像应该的那样清楚。我想出来了。我添加了一个按钮来浏览我或用户计算机上的文件。谢谢你的努力。我将发布我的代码。您的解决方案似乎让用户浏览该文件。如果要确保用户正在打开正确的文件。如果你用tkinter应用程序打包文件,你可能会强迫用户打开正确的文件,因为它们的路径总是相同的。你的帖子格式不正确。你能花几分钟来修复它吗?如果只是为了你,那没关系,但是如果你让其他人接触代码,你应该把你所有的导入放在模块的顶部。有关格式设置的详细信息,请查看此链接: