Pygame--表格组件(一)

        这一期主要讲表格组件的构建,首先看下效果图。

        

         上面是一个简单的调用,实现了简单的查询跟修改功能,这一期主要讲表格组件的绘制函数。

        (一)定义类以及一些需要使用的参数

class TableView:
    def __init__(self,x, y, width, height, data, DP=None):
        # 组件定位
        self.x = x
        self.y = y
        self.width = width
        self.height = height

        # 接收数据
        self.data = data.data
        self.header = data.header
        self.header.append("")
        
        # 表格基本配置
        self.limit = 10 # 每页的数据量
        self.row_height = height / (self.limit+1) # 行高度
        self.limit_data = self.split_list(self.data, 10) # 切割表格数据进行分页
        
        # 点击事件以及分页的参数
        self.active = None 
        self.change_data = None
        self.dialog_type = False
        self.page = 0
        self.page_index = 0
        self.buffer_item = self.limit_data[self.page]
        self.line_positions = [self.row_height * i + self.y for i in range(1, len(self.buffer_item)+2)]
        self.page_limit = self.split_list([x for x in range(len(self.limit_data))], 5)
        self.page_limit_flag = 0
        self.page_recv = ["<"]+[x+1 for x in self.page_limit[self.page_limit_flag]]+[">"]
        self.page_active = 1
        self.gap_size = 10

        # 创建坐标点列表
        self.page_options = self.create_page_option()

        # 循环定义dialog内的输入框
        input_width = 300
        input_height = 30
        input_init_x = (self.x+((self.width-(self.width*0.8)))/2)+ ((self.width*0.8 -input_width)//2)
        input_init_y = self.y  + 30
        font = pygame.font.Font(r"C:\workspace\成绩管理系统\ui\style\SimSun.ttf", 16)
        self.rest_button = pygame.Surface((60, 30))
        self.push_button = pygame.Surface((60,30))
        self.input_view = []
        for i in range(len(self.header)-1):
            exec(f'self.input_view.append(TextBox({input_width}, {input_height}, {input_init_x}, {input_init_y + i*60},font=font,font_color=('
                 f'255,255,255),dest_offst={input_height+5}))')

        (二)绘制表格横线跟竖线

    def draw(self, screen,left_and_right_border=False, border_color=(0, 0, 0)):
        next_h = 0
        next_v = 0 if left_and_right_border else self.width/(len((self.buffer_item[0]))+1)
        v_range = len(self.header)+1 if left_and_right_border else len(self.header)-1
        # 绘制表格横线
        for i in range(len(self.buffer_item)+2):
            pygame.draw.aaline(screen, border_color, (self.x, self.y+next_h), (self.x+self.width, self.y+next_h))
            next_h += self.row_height
        # 绘制表格竖线
        for j in range(v_range):
            pygame.draw.aaline(screen, border_color, (self.x+next_v, self.y),(self.x+next_v, self.y+self.height))
            next_v += self.width/len(self.header)

        表格中接受了几个参数,screen代表主类的窗口对象,left_and_right_border代表左右是否需要边框,border_color这个就是边框颜色。

        (三)绘制数据到表格

    def draw_data(self, screen, font_color=(0, 0, 0),header_color=(255,0,0),onclick_text=(0,0,255)):
        font = pygame.font.Font(r"SimSun.ttf", 15)
        next_y = self.row_height
        col_size = self.width/(len(self.header))

        # 写入表头
        header_x = 0
        for i in self.header:
            header_font = pygame.font.Font(r"SimSun.ttf", 25)
            text = header_font.render(str(i), True, header_color)
            text_width, text_height = text.get_width(),text.get_height()
            x = self.x  + (col_size / 2 - (text_width / 2)) + header_x
            y = self.y  + (self.row_height / 2 - (text_height / 2))
            screen.blit(text,(x,y))
            header_x += col_size

        # 写入数据
        for i in range(len(self.buffer_item)):
            next_x = 0
            for j in range(len(self.buffer_item[i])):
                text = font.render(str(self.buffer_item[i][j]), True, font_color)
                text_width = text.get_width()
                text_height = text.get_height()
                if text_width > col_size:
                    t = str(self.buffer_item[i][j])[:5]+'...'
                    text = font.render(t, True, font_color)
                    text_width = text.get_width()
                    text_height = text.get_height()
                x = self.x + next_x + (col_size/2 - (text_width/2))
                y = self.y + next_y + (self.row_height/2 - (text_height/2))
                screen.blit(text, (x, y))
                next_x += col_size
            text = font.render("查看详情", True, onclick_text)
            text_width = text.get_width()
            text_height = text.get_height()
            x = self.x + next_x + (col_size / 2 - (text_width / 2))
            y = self.y + next_y + (self.row_height / 2 - (text_height / 2))
            screen.blit(text, (x, y))
            next_y += self.row_height

        这个函数现在做了一个基本的数据展示功能,这一期就讲到这里,下一期主要讲dialog框、分页和点击事件处理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值