0

为什么这段代码不起作用?我认为这与 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()
4

1 回答 1

0

您的错误是因为您尝试x1在本地上下文 ( callback1) 中删除全局变量 (),而之前没有将其声明为全局变量。

你可以这样做:

global x1
del x1

但是您要完成的工作有一个更普遍的问题,当您说 时text = x1,它只是将 的当前值传递x1给您创建的text属性Label,而更改x1之后的值对此没有影响,您想要的是将 widget.text 更改为新值(并且您应该在这些回调中重命名object为,是 python 中基类的名称,因此您不应将其用作参数或任何东西)。widgetobject

此外,从长远来看,您构建代码的方式不会很好地工作,您应该在类的方法中做更多的事情(您之前所做的大部分事情都可以在build.

于 2013-08-18T00:13:03.887 回答