2016-01-21 25 views
-1

我在python(使用tkinter)進行測驗。由於我製作了一個名爲「分數」的變量,因此我被卡在分數系統上,但是當您嘗試向分數添加+1時,就會出現錯誤。我曾嘗試在過程之前對其進行定義,但仍然無效。Python的Tkinter測驗分數系統

import tkinter as tk 
window = tk.Tk() 
window.title("6 Questions") 
window.geometry("500x150") 
def inst(): 
    t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.") 
    t.pack() 
def start(): 
    score = 0 
    q1 = tk.Tk() 
    q1.title("question 1") 
    q = tk.Label(q1, text="what type of input holds whole numbers?") 
    q.pack() 
    a = tk.Label(q1, text="1.) int") 
    a.pack() 
    b = tk.Label(q1, text="2.) string") 
    b.pack() 
    c = tk.Label(q1, text="3.) float") 
    c.pack() 
    ans = tk.Entry(q1, width=40) 
    ans.pack() 
    def qu1(): 
     deranswer = ans.get() 
     if deranswer == "1" or deranswer == "int": 
      crr = tk.Label(q1, text="Correct!", fg="green") 
      crr.pack() 
      score = score + 1 
      q2 = tk.Tk() 
      q2.title("Question 2") 
      qw = tk.Label(q2, text="Choose the correct code:") 
      qw.pack() 
      ans21 = tk.Label(q2, text="1.) print hello world") 
      ans21.pack() 
      ans32 = tk.Label(q2, text="2.) print hello world()") 
      ans32.pack() 
      ans43 = tk.Label(q2, text="3.) print('hello world')") 
      ans43.pack() 
      ans2 = tk.Entry(q2, width=40) 
      ans2.pack() 
      def qu2(): 
       deranswer = ans2.get() 
       if deranswer == "3" or deranswer == "print('hello world')": 
        crr = tk.Label(q2, text="Correct!", fg="green") 
        crr.pack() 
        score = score + 1 
        q3 = tk.Tk() 
        q3.title("Question 3") 
        qu3 = tk.Label(q3, text="What is a variable?") 
        qu3.pack() 
        ans56 = tk.Label(q3, text="1.) Something that stores food") 
        ans56.pack() 
        ans65 = tk.Label(q3, text="2.) A name that stores a piece of data") 
        ans65.pack() 
        ans45 = tk.Label(q3, text="3.) Displays a word") 
        ans45.pack() 
        ans25 = tk.Entry(q3, width=40) 
        ans25.pack() 
        def qu2(): 
         deranswer = ans25.get() 
         if deranswer == "2" or deranswer == "A name that stores a piece of data": 
          crr = tk.Label(q3, text="Correct!", fg="green") 
          crr.pack() 
          score = score + 1 
          dis = tk.Label(q3, text=score, fg="blue") 
         else: 
          incr = tk.Label(q3, text="Incorrect!", fg="red") 
          incr.pack() 
        sub = tk.Button(q3, text="Submit", command=qu2) 
        sub.pack() 
       else: 
        incr = tk.Label(q2, text="Incorrect!", fg="red") 
        incr.pack() 
      sub = tk.Button(q2, text="Submit", command=qu2) 
      sub.pack() 
     else: 
      incr = tk.Label(q1, text="Incorrect!", fg="red") 
      incr.pack() 
    sub = tk.Button(q1, text="Submit", command=qu1) 
    sub.pack() 


greet = tk.Label(window, text="Welcome to the 6 Question Quiz.") 
greet.pack() 
start = tk.Button(window, command=start, text="Start") 
start.pack() 
instr = tk.Button(window, text="Instructions", command=inst) 
instr.pack() 
end = tk.Button(window, text="Exit", command=exit) 
end.pack() 
+1

你說什麼「得到一個錯誤」後面的理由是什麼?但是不告訴我們那個錯誤是什麼? –

+0

異常Tkinter回調 回溯(最近一次調用最後一次): 文件「C:\ Python33 \ lib \ idlelib \ run.py」,行109,主 seq,request = rpc.request_queue.get(block = True ,timeout = 0.05) 在處理上述異常期間,發生了另一個異常: Traceback(最近一次調用),第175行,在獲得 舉起空 隊列。空 最後): 文件「C:\ Python33 \ lib \ tkinter \ __ init__.py」,行1475,在__call__中 return self.func(* args) 文件「N:\ menu.py」,第27行,在qu1 得分=得分+ 1 UnboundLocalError:局部變量'得分'參考在分配之前進行分配 – morgan

+0

您的代碼中是否真的有這麼多的嵌套函數定義,或者您在粘貼時遇到了麻煩? – TigerhawkT3

回答

0

從回溯您的評論:

line 27, in qu1 score = score + 1 UnboundLocalError: local variable 'score' referenced before assignment

您嘗試添加score+1本身,但score未在函數中定義。變量有一定的範圍。在the Python documentation中瞭解更多關於它的信息。