0

このコードが機能しないのはなぜですか? b1をクリックするたびに「UnboundLocalError:割り当て前に参照されたローカル変数 'x1'」というエラーが表示されるため、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()
4

1 に答える 1

0

あなたのエラーは、以前にグローバルとして宣言せずx1に、ローカル コンテキスト ( ) でグローバル変数 ( )を削除しようとしたためです。callback1

あなたがすることができます:

global x1
del x1

しかし、あなたが達成しようとしていることには、より一般的な問題text = x1があります。 widget.text を新しい値に変更します (これらのコールバックで名前を変更する必要があります。これは Python の基本クラスの名前であるため、パラメーターなどとして使用しないでください)。x1textLabelx1objectwidgetobject

また、コードを構造化する方法は長期的にはうまく機能しません。クラスのメソッドでより多くのことを行う必要があります (以前に行っていたことのほとんどは で行うことができますbuild)。

于 2013-08-18T00:13:03.887 に答える