如何在此代码中正确使用回调? (Kivy)

时间:2013-08-17 14:31:03

标签: python callback kivy

为什么这段代码不起作用?我认为这与已定义的x1有关,因为每当我点击b1时,我都会收到错误“UnboundLocalError:局部变量'x1'在分配前引用”。请我在整个互联网上搜索没有运气....抱歉,我对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()

1 个答案:

答案 0 :(得分:0)

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

你可以这样做:

global x1
del x1

但是,您要尝试完成的内容存在一个更普遍的问题,当您说text = x1时,它只会将x1的当前值传递给text的{​​{1}}属性{1}}您创建并稍后更改Label的值对此没有影响,您想要的是将widget.text更改为新值(您应该将x1重命名为{{ 1}}在这些回调中,object是python中基类的名称,所以你不应该将它用作参数或任何东西)。

另外,从长远来看,构建代码的方式不会很好,你应该在类的方法中做更多的事情(你以前做过的大部分工作都可以在widget完成)。< / p>