示例#1
0
class GoodsTable(tk.Toplevel):
    def __init__(self,update_combo,show=None):
        '''
        :param show:触发主界面的数据显示
        '''
        super().__init__()
        self.resizable(0, 0)  # 阻止Python GUI的大小调整
        self.title("选择商品")
        self.goodsId = {}
        self.show = show        # 将数据载入表格管理器
        self.update_combo = update_combo      # 更新常用组合
        self.initFrame()
        self.add_data()
        self.mainloop()
    def initFrame(self):
        # *****界面区*****
        # 布局参数
        params = ['row','column','rowspan','columnspan']
        gridMap =  {'label':(0,0),
                'entry':(0,1),
                'search_button':(0,2),
                'table':(1,1,None,3),
                'next_page':(2,1),
                'modify':(2,2),
                'corfirm':(2,3)}
        cnfs = {}
        for k,vals in gridMap.items():
            dic = {}
            for i,v in enumerate(vals):
                dic[params[i]] = v
            cnfs[k] = dic
        ttk.Label(self, text='搜索', style="BW.TLabel").grid(cnfs['label'])

        self.keyword = tk.StringVar()   # 搜索框
        tk.Entry(self, borderwidth=3, width=40, textvariable=self.keyword, selectbackground='gray').grid(cnfs['entry'])
        ttk.Button(self, text='搜索', command="", width=9).grid(cnfs['search_button'])
        frame = tk.Frame(self)
        frame.grid(cnfs['table'])
        model = TableModel()
        self.table = TableCanvas(frame,model)
        self.table.show()
        ttk.Button(self, text='下一页', command="", width=9).grid(cnfs['next_page'])
        ttk.Button(self, text='设为组合', command=self.set_combo, width=9).grid(cnfs['next_page'],sticky=tk.E)
        ttk.Button(self, text='修改', command=self.modify, width=9).grid(cnfs['modify'])
        ttk.Button(self, text='选择', command=self.choic, width=9).grid(cnfs['corfirm'])

    def set_combo(self):
        comboName = askstring("设置常用组合", "组合名称", initialvalue='笔记本')   # 组合名称
        goodsId = []
        for id in self.table.get_selectedRecordNames():
            goodsId.append(self.goodsId[id])
        combo= self.update_combo(comboName,goodsId)     # 更新常用组合
        with open('data/combo.json','w',encoding='utf-8') as f: # 存入文件
            json.dump(combo,f)
        self.choic()

    def add_data(self):
        '''加载配置、数据'''
        iurls = Curls()
        self.data = {}
        data = {}
        for i,inst in enumerate(iurls.query()):
            self.goodsId[i] = (inst.id,inst.goods)
            dic = {}
            dic['goods'] = inst.goods
            dic['common_price'] = inst.common_price
            dic['expect_price'] = inst.expect_price
            dic['setting'] = inst.setting
            data[i] = dic
            self.data[i] = dic.copy()
        self.table.model.importDict(data)

    def choic(self):
        try:
            if self.show is not None:
                lGoodsId = []
                for id in self.table.get_selectedRecordNames():
                    lGoodsId.append(self.goodsId[id])
                self.show(lGoodsId)
            self.destroy()
        except IndexError:
            tk.messagebox.showinfo('提示', '重新选择')
    def modify(self):
        iurls = Curls()
        for k,vals in self.table.model.data.items():
            if self.data[k] != vals:
                iurls.update(f'id={self.goodsId[k][0]}',**vals)