为什么这段代码不起作用?我认为这与 x1 已经定义有关,因为每当我单击 b1 时都会收到错误“UnboundLocalError: local variable 'x1' referenced before assignment”。请我搜索了整个互联网,但没有运气......对不起,我对 Python 和编程比较陌生。
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()