2013-08-17 37 views
0

這段代碼爲什麼不起作用?我認爲這與x1已經被定義是有關的,因爲當我點擊b1時,我得到錯誤「UnboundLocalError:本地變量'x1'在賦值之前引用」。請我沒有運氣搜索整個互聯網....對不起,我對Python和編程相對較新。如何在此代碼中正確使用回調? (Kivy)

import calendar 
import datetime 
from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.label import Label 

now = datetime.datetime.now() 
h = now.hour 
m = now.minute 
s = now.second 
year = now.year 
month = now.month 
day = now.day 
home = 'home.' 
weekday1 = calendar.weekday(year, month, day) 
if len(str(m)) == 1: 
    zero = '0' 
else: 
    zero = '' 
if len(str(s)) == 1: 
    zero1 = '0' 
else: 
    zero1 = '' 

if weekday1 == 0: 
    day = 'Monday' 
    time = '''Period 1/2/3/4 = History 
Period 5/6 = Japanese''' 
    if h == 8 and m >= 40: 
    current = 'Homeroom.' 
    elif h == 9 or (h == 10 and m <= 40): 
    current = 'History.' 
    elif h == 10 and m > 40: 
    current = 'recess.' 
    elif h == 11 or (h == 12 and m <= 40): 
    current = 'History.' 
    elif (h == 12 and m > 40) or (h == 13 and m <= 20): 
    current = 'lunch.' 
    elif (h == 13 and m > 20) or h == 14: 
    current = 'Japanese.' 
    else: 
    current = home 
elif weekday1 == 1: 
    day = 'Tuesday' 
    time = '''Period 1 = English 
Period 2 = Maths 
Period 3/4 = English 
Period 5/6 = ICT''' 
    if h == 8 and m>= 40: 
    current = 'Homeroom.' 
    elif h == 9 and m <= 50: 
    current = 'English.' 
    elif (h == 9 and m > 50) or (h == 10 and m <= 40): 
    current = 'Maths.' 
    elif h == 10 and m > 40: 
    current = 'recess.' 
    elif h == 11 or (h == 12 and m <= 40): 
    current = 'English.' 
    elif (h == 12 and m > 40) or (h == 13 and m <= 20): 
    current = 'lunch.' 
    elif (h == 13 and m > 20) or h == 14: 
    current = 'ICT.' 
    else: 
    current = home 
elif weekday1 == 2: 
    day = 'Wednesday' 
    time = '''Period 1/2 = Science Extended 
Period 3 = English 
Period 4 = Maths 
Period 5/6 = Science''' 
    if h == 8 and m >= 40: 
    current = 'Homeroom.' 
    elif h == 9 or (h == 10 and m <= 40): 
    current = 'Science Extended.' 
    elif h == 10 and m > 40: 
    current = 'recess.' 
    elif h == 11 and m <= 50: 
    current = 'English.' 
    elif (h== 11 and m > 50) or (h == 12 and m <= 40): 
    current = 'Maths.' 
    elif (h == 12 and m > 40) or (h == 13 and m <= 20): 
    current = 'lunch.' 
    elif (h == 13 and m > 20) or h == 14: 
    current = 'Science.' 
    else: 
    current = home 
elif weekday1 == 3: 
    day = 'Thursday' 
    time = '''Period 1/2 = Art 
Period 3 = Science 
Period 4 = Wellbeing 
Period 5 = English 
Period 6 = Maths''' 
    if h == 8 and m >= 40: 
    current = 'Homeroom.' 
    elif (h == 10 and m <= 40) or h == 9: 
    current = 'Art.' 
    elif h == 10 and m > 40: 
    current = 'recess.' 
    elif h == 11 and m <= 50: 
    current = 'Science.' 
    elif (h == 11 and m > 50) or (h == 12 and m <= 40): 
    current = 'Wellbeing.' 
    elif (h == 12 and m > 40) or (h == 13 and m < 20): 
    current = 'lunch.' 
    elif (h == 13 and m >= 20) or (h == 14 and m <= 10): 
    current = 'English.' 
    elif h == 14 and m > 10: 
    current = 'Maths.' 
    else: 
    current = home 
elif weekday1 == 4: 
    day = 'Friday' 
    time = '''Period 1/2 = PE 
Period 3 = English 
Period 4 = Maths 
Period 5/6 = Music''' 
    if h == 8 and m >= 40: 
    current = 'Homeroom.' 
    elif h == 9 or (h == 10 and m <= 40): 
    current = 'PE.' 
    elif h == 10 and m > 40: 
    current = 'recess.' 
    elif h == 11 and m <= 50: 
    current = 'English.' 
    elif (h == 11 and m > 50) or (h == 12 and m <= 40): 
    current = 'Maths.' 
    elif (h == 12 and m > 40) or (h == 13 and m < 20): 
    current = 'lunch.' 
    elif (h == 13 and m >= 20) or h == 14: 
    current = 'Music.' 
    else: 
    current = home 
else: 
    day = 'a weekend' 
    time = 'You have none.' 
if day == 'a weekend': 
    a = "You don't have to be anywhere." 
else: 
    a = ('You should be at ' + current) 

a1 = ('Today is ' + day + '.') 
a2 = ('''Today your timetable is: 
''' + time) 
a3 = ('The current time is ' + str(h) + ':' + zero + str(m) + ':' + zero1 + str(s) + '.') 

t1 = 'What is the day today?' 
t2 = 'What is the current time?' 
t3 = 'What is my timetable today?' 
t4 = 'Where should I be?' 

x1, x2, x3, x4 = '', '', '', '' 

def callback1(object): 
    del x1 
    x1 = a1 
def callback2(object): 
    x2 = a3 
def callback3(object): 
    x3 = a2 
def callback4(object): 
    x4 = a 

b1 = Button(text = t1) 
b1.bind(on_press = callback1) 

layout = GridLayout(cols = 2) 
layout.add_widget(b1) 
layout.add_widget(Label(text = x1)) 
layout.add_widget(Button(text = t2)) 
layout.add_widget(Label(text = x2)) 
layout.add_widget(Button(text = t3)) 
layout.add_widget(Label(text = x3)) 
layout.add_widget(Button(text = t4)) 
layout.add_widget(Label(text = x4)) 

class TimeTable(App): 
    def build(self): 
    return layout 

if __name__ == '__main__': 
    TimeTable().run() 

回答

0

你的錯誤是因爲你嘗試刪除在本地範圍內(callback1)一個全局變量(x1),沒有前宣佈它作爲全球性的。

你可以這樣做:

global x1 
del x1 

但還有一個更普遍的問題與您正在試圖完成,當你說text = x1,它只是傳遞的x1當前值到Labeltext性質是什麼你創建,並且改變x1後面的值對此沒有任何影響,你想要的是將widget.text更改爲新值(並且你應該在這些回調中將object重命名爲widgetobject是基類的名稱,在python,所以你不應該用它作爲參數或任何東西)。另外,從長遠來看,你構造代碼的方式不會奏效,你應該在你的班級的方法中做更多的事情(大部分你以前做的事情都可以在build中完成)。