如何从类中调用函数?

时间:2018-06-09 01:16:59

标签: python function class methods

我是Python的新手,但我已经找到了一些很酷的东西。我最近编写了几个类来为应用程序绘制按钮/文本框,以便在工作时打印标签。 我需要在我的sql server上循环浏览一些记录 - 连接工作正常,所以我不会发布所有代码。

我有一个函数getNextPart(),它调用我的sql server上的表中的下一个项目 - 意图是当我点击我的" +"旋转按钮,它运行getnextpart()并将其分配给全局参数。当我点击" - " getprevpart()时会发生同样的情况。按钮。

当我点击" +"按钮处理程序事件按预期执行所有操作,但根本不运行getnextpart()功能。它没有运行print语句或任何东西,所以我怀疑它不是数据库连接器坏了。也没有报告错误。我尝试将getnextpart作为一种方法,并将其称为self.getnextpart(),但这对我来说也不起作用。我已经搜索了几天但找不到任何东西,所以我在这里请求一些指导。

如何(甚至可以)从handle_event()类的SpinnerButton方法中调用函数?

这是我的程序,不包括mysql连接器和在开始时初始化的其他参数。

# Initialise global variables for display on the screen
partnumber = ''

def getnextpart():

    global partnumber

    print(str('Get Next Part should run here !!'))
    dbcurs=cnx.cursor()
    cnx.commit()
    query = 'spGetNextItem'
    args=[partnumber]
    result_args = dbcurs.callproc(query ,args)

    for result in dbcurs.stored_results():
        itemresult = result.fetchone()
        itemcode = str(itemresult[0])

    partnumber = itemcode
    print(partnumber)

def getprevpart():

    global partnumber

    dbcurs=cnx.cursor()
    cnx.commit()
    query = 'spGetPrevItem'
    args=[partnumber]
    result_args = dbcurs.callproc(query ,args)

    for result in dbcurs.stored_results():
        itemresult = result.fetchone()
        itemcode = str(itemresult[0])

    partnumber = itemcode
    print(partnumber)

class SpinnerButton:

    def __init__(self,x,y,w,h,type,cause):
        self.rect = pg.Rect(x,y,w,h)
        self.color = light_blue
        if type == 'UP':
            self.text = '+'
            self.x_adj = 7
            self.y_adj = 2
        elif type == 'DOWN':
            self.text = '-'
            self.x_adj = 11
            self.y_adj = 5
        else:
            self.txt = '>'
        self.sptorun = cause
        self.dir = type
        self.txt_surface = myfont30.render(self.text, True, black)

    def handle_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                print(str(self.sptorun))
                print(str(self.dir))
                # Toggle the active variable.
                if self.sptorun =='items' and self.dir == 'UP':
                    getnextpart()
                elif self.sptorun == 'items' and self.dir =='DOWN':
                    getprevpart()

        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:
                    print(self.text)
                    self.text = ''
                elif event.key == pg.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def draw(self, screen):
        # Blit the rect.
        pg.draw.rect(screen, self.color, self.rect, 0)
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x+self.x_adj, self.rect.y - self.y_adj))




class InputBox:

    def __init__(self, x, y, w, h, text, lbltxt):
        self.rect = pg.Rect(x, y, w, h)
        self.color = COLOR_INACTIVE
        self.text = text
        self.lbltext = lbltxt
        self.txt_surface = myfont25.render(text, True, blue)
        self.lbltxt_surface = myfont25.render(lbltxt, True, red)
        self.active = False

    def handle_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN:
            # If the user clicked on the input_box rect.
            if self.rect.collidepoint(event.pos):
                # Toggle the active variable.
                self.active = not self.active
            else:
                self.active = False
            # Change the current color of the input box.
            self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
        if event.type == pg.KEYDOWN:
            if self.active:
                if event.key == pg.K_RETURN:
                    print(self.text)
                    self.text = ''
                elif event.key == pg.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                # Re-render the text.
                self.txt_surface = FONT.render(self.text, True, self.color)

    def update(self):
        # Resize the box if the text is too long.
        width = max(200, self.txt_surface.get_width()+10)
        self.rect.w = width

    def draw(self, screen):
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+2))
        screen.blit(self.lbltxt_surface, (self.rect.x - self.lbltxt_surface.get_width()- 10, self.rect.y))
        # Blit the rect.
        pg.draw.rect(screen, self.color, self.rect, 2)






def main():
    global partnumber
    clock = pg.time.Clock()
    input_partnumber = InputBox(200, 10, 140, 32, str(partnumber), 'Item: ')
    input_boxes = [input_partnumber]
    spin_itemup = SpinnerButton(400,10,32,33,'UP','item')
    spin_itemdown = SpinnerButton(433,10,32,33,'DOWN','item')
    spin_buttons = [spin_itemup, spin_itemdown]
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)

            for btn in spin_buttons:
                btn.handle_event(event)

        for box in input_boxes:
            box.update()

        # screen.fill((30, 30, 30))
        screen.fill(bg)
        for box in input_boxes:
            box.draw(screen)

        for btn in spin_buttons:
            btn.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pg.quit()

0 个答案:

没有答案