我需要一些帮助我的代码,我使用两个不同的功能,一个用于选择数据库上的数据,另一个用于通过键盘控制向右移动。我想减少代码,以避免在一个函数中添加相同的代码行。
然而,当我尝试这个时:
import move_right
class MyClass(xbmcgui.WindowXML):
def GoRight(self):
move_right.GoRight(self)
我会收到错误:NameError:全局名称' program_stop_minutes'未定义。
错误是跳到这一行:
if program_stop_minutes == '05':
这是发现错误的move_right.py脚本:
import datetime
import time
import xbmc
import xbmcgui
prog_id = 0
select_db_flag = False
def select_db(self):
program_button = [elem.control for elem in self.program_buttons]
programs_id = list()
program_width = list()
positions_X = list()
positions_Y = list()
for elem in program_button:
programs_id.append(elem.getId())
positions_X.append(elem.getX())
positions_Y.append(elem.getY())
program_width.append(elem.getWidth())
programs_id = map(str, programs_id)
posX = map(str, positions_X)
posY = map(str, positions_Y)
program_width = map(str, program_width)
for pos_X, pos_Y, prog_id, prog_width in zip(positions_X, positions_Y, programs_id, program_width):
if select_db_flag == True:
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 'source.db'))
conn1 = database.connect(profilePath)
cur1 = conn1.cursor()
cur1.execute('SELECT stop_date FROM programs where button_id=?', [prog_id])
stop_date = cur1.fetchone()
if stop_date is not None:
stop_date = str(stop_date[0])
stop_time = time.strptime(str(stop_date), '%Y%m%d%H%M%S')
stop_time = datetime.datetime.fromtimestamp(time.mktime(stop_time))
program_stop_hours = stop_time.strftime('%H')
program_stop_minutes = stop_time.strftime('%M')
program_AM_PM = stop_time.strftime('%p')
half_hour = str(self.getControl(344).getLabel())
one_hour = str(self.getControl(345).getLabel())
one_hour_half = str(self.getControl(346).getLabel())
epg_time_1 = time.strptime(half_hour, '%I:%M%p')
epg_time_2 = time.strptime(one_hour, '%I:%M%p')
epg_time_3 = time.strptime(one_hour_half, '%I:%M%p')
program_stop_time = program_stop_hours +':' + program_stop_minutes + program_AM_PM
program_stop_time = time.strptime(program_stop_time, '%I:%M%p')
def GoRight(self):
if int(pos_X) == 616 and int(prog_width) == 515:
if program_stop_minutes == '05':
programs_width = 399
self.getControl(int(prog_id)).setWidth(int(programs_width))
nextprogram = int(prog_id) + 1
nextprogramX = self.getControl(int(nextprogram)).getX()
nextprogram_width = self.getControl(int(nextprogram)).getWidth()
self.getControl(int(nextprograms)).setPosition(780, int(pos_Y))
self.select_db()
select_db_flag = True
当我想从program_stop_minutes
函数调用以获取timedelta的返回输出时,我想定义select_db
,但我不知道如何执行此操作。
当我想从program_stop_minutes
函数打电话时,如果我想获得timedelta的返回输出,我能帮助我如何定位select_db
吗?
答案 0 :(得分:0)
正如错误消息明确指出的那样,program_stop_minutes
不是全局变量。您可以在select_db
内定义它,因此在该函数的范围内只能 。如果要从其他函数(例如GoRight
)访问它,则需要在全局范围中定义变量或从函数中返回值。
全球范围
program_stop_minutes = 0
def select_db():
global program_stop_minutes
# Do stuff here then modify program_stop_minutes
program_stop_minutes = stop_time.strftime('%M')
def GoRight():
global program_stop_minutes
if program_stop_minutes == '05':
# Do some other stuff
从select_db
如果您想要从select_db
返回信息而不是使用全局变量,则可以执行以下操作:
def select_db():
program_stop_minutes = stop_time.strftime('%M')
return program_stop_minutes
def GoRight():
program_stop_minutes = select_db()
if program_stop_minutes == '05':
# Do some other stuff
如果您需要从函数返回的多个值,则可以将其作为元组返回。您可以通过这种方式返回任意数量的参数。
def select_db():
program_stop_minutes = stop_time.strftime('%M')
epg_time_1 = time.strptime(half_hour, '%I:%M%p')
return (program_stop_minutes, epg_time_1)
def GoRight():
program_stop_minutes, epg_time_1 = select_db()
if program_stop_minutes == '05':
# Do some other stuff
作为旁注,我不确定为什么你在整个地方使用self
变量。你认为这些都是班级的方法吗?正如你目前所写的那样,它们不是。它们只是功能。