Tkinter,更新GUI(mainloop(),after()update())

时间:2018-12-12 17:08:53

标签: python user-interface tkinter mainloop

我有一个Rabbitmq使用者接收一些数据,在同一脚本中,我在另一个tkinter文件中导入了一个类。

下一个代码是接收RabbitMQ数据并创建tkinter实例的方法。

from viewver import EntryGrid
    def consume_message(body):
        body = body.decode('ascii')
        body = body.replace("\'", "")
        data = json.loads(body)
        mGui = EntryGrid(colList=['Bids', 'Asks'], rowList=10)
        mGui.viewver(data)

下一个代码块是在tkinter类中接收Rabbit数据的方法。

def viewver(self, data = None):
        q = queue.Queue()
        if data != None:    
            q.put(data)
        while not q.empty():
            self.buildGrid(data = q.get()

在这里,我该如何更新GUI。

def buildGrid(self, data, state = None):

        #if state != None:
            #self.__init__(colList = ['Bids', 'Asks'], rowList = 10)
            #self = state

        self.make_header()

        canvas = tkinter.Canvas(self.canvasFrame, width=818)
        canvas.grid(row=0, column=0, sticky="news")
        canvas.delete("all")

        vsb = tkinter.Scrollbar(self.canvasFrame, orient="vertical", command=canvas.yview)
        vsb.grid(row=0, column=1, sticky='ns')
        hsb = tkinter.Scrollbar(self.canvasFrame, orient="horizontal", command=canvas.xview)
        hsb.grid(row=1, column=0, sticky=tkinter.EW)

        canvas.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)

        self.entriesFrame = tkinter.Frame(canvas)
        self.entriesFrame.config(width=1000)
        canvas.create_window((0, 0), window = self.entriesFrame, anchor='nw')
        sizeLen = self.maxLen(data)
        bid = data['bid']
        ask = data['ask']

        for i in range(2):
            for j in range(len(sizeLen)):
                if i == 0:
                    try:
                        EntryWidget(self.entriesFrame, 0, j, str(bid[j]['price']) + "               " + str(bid[j]['amount1']/1000))
                    except:
                        EntryWidget(self.entriesFrame, i, j, "")
                elif i == 1:
                    try:
                        EntryWidget(self.entriesFrame, i, j, str(ask[j]['amount1']/1000) + "               " + str(ask[j]['price']))
                    except:
                        EntryWidget(self.entriesFrame, i, j, "")    

        #self.update()
        self.entriesFrame.update_idletasks()

        first5columns_width = 250
        first5rows_height = 500
        self.canvasFrame.config(width=first5columns_width + vsb.winfo_width(),
                    height=first5rows_height)

        canvas.config(scrollregion=canvas.bbox("all"))
        #time.sleep(4)        
        #self.destroy()
        self.after(1000, self.viewver)
        self.mainloop()
        #return self, True

谢谢。

0 个答案:

没有答案