2013-11-20 59 views
2

是否有可能允許一個程序用戶,他們已經從程序哪個測試應該使用得到的答案後,上傳自己的數據?我知道我需要使用tkFileDialog,但是在程序的第一部分運行後遇到問題。讓我知道如果這不清楚。我的代碼到目前爲止是:的Tkinter的FileDialog

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

定義 「上傳」。在用戶從文件對話框中選擇一個文件後應該發生什麼? – Kevin

+0

長期目標是利用它們才能運行什麼都統計程序,他們被告知,最適合他們的數據所選擇的文件。現在,我需要能夠讓他們選擇他們想使用的文件。 – kellie92

+0

你想把所有的問題都得到了回答後有開什麼2-道具測試?如果是這樣,這些存儲在每個人都可以訪問的位置(例如服務器或每個人使用一臺計算機)? – Benjooster

回答

0

我想通了我自己,從課程的互聯網一定的幫助。我更新的代碼是:

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

您的帖子格式不正確。你能花幾分鐘時間來解決它嗎? –

+0

如果只是爲了你,那沒關係,但是如果你有其他人觸摸代碼,你應該把你所有的輸入放在模塊的頂部。查看更多關於格式的信息:http://www.python.org/dev/peps/pep-0008/#imports – Benjooster

0

如果你需要打開的文件是從一箇中央位置訪問,你可以有你的if聲明打開他們需要爲他們的文件。

嘗試設置這些文件作爲變量:

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

等爲您的文件的其餘部分。

然後,而不是告訴他們該怎麼打開剛纔與os.system()打開它:

os.system(t_test)

不要忘記import os與進口的其餘部分。

+0

不是真的,這是我的錯,因爲我的問題是不明確的,因爲它應該是。我想到了。我添加了一個按鈕來瀏覽我的電腦或用戶電腦上的文件。謝謝你嘗試。我會發布我的代碼。 – kellie92

+0

看來你的解決方案有用戶瀏覽文件。如果你想確保用戶正在打開正確的文件。如果你用tkinter應用程序打包文件,你可以強制用戶打開正確的文件,因爲它們的路徑總是相同的。 – Benjooster