亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 和 Glade 在 GTK3 中刷新 matplotlib 圖表

如何使用 Python 和 Glade 在 GTK3 中刷新 matplotlib 圖表

MMTTMM 2021-09-14 21:10:22
我已經使用 GTK3、Python 和 Glade 將兩個 matplotlib 圖插入到一個容器中。他們使用來自第三方 API 的數據繪制區域的天氣。我希望在我輸入新的語言環境并按下刷新按鈕時更新圖表。下面的每個解決方案都以不同的問題結束。太多了無法一一描述。通常,他們設法用新圖表顯示主窗口的另一個實例,但帶有舊圖表的舊實例保持打開狀態。我試過了:銷毀父容器,然后重新加載更新的容器。以下代碼產生分段錯誤。代碼嘗試將對象從構建器的新實例傳遞給舊實例。我相信這是我的主要問題。我不知道如何將我正在使用的構建器的實例傳遞到其中,on_refresh_button_click()而不必將所有內容都重寫到一個類中。def on_refresh_button_click(self, widget):    parent = self.get_parent()    grandparent = parent.get_parent()    parent.destroy()    city_name = "yerevan"    db = "test.db"    get_updated_data(city_name)    builder = builder_with_signals()    read_weather_from_db(db, builder, city_name)    grandparent.add(parent)    parent.show_all()使用self.get_parent()獲取按鈕的父容器為對象上下工夫。這幾乎奏效了,我想。我可以remove()和destroy()包含圖形的容器。我想我也成功添加了更新的。但我無法讓它顯示出來。編碼:def on_refresh_button_click(self, widget):    parent = self.get_parent()    city_name = "yerevan"    db = "test.db"    get_updated_data(city_name)    fig_list = read_weather_from_db(db, city_name)    for child in parent:        try:            for grandchild in child:                if Gtk.Buildable.get_name(grandchild) == "chart_past":                    parent = child # Confusing, yes.                    old = grandchild                    new = FigureCanvas(fig_list[0])                    props = {}                    for key in parent.list_child_properties():                        props[key.name] = parent.child_get_property(old, key.name)                    parent.remove(old)                    parent.add(new)                    for name, value in props.items():                        parent.child_set_property(new, name, value)                    parent.show_all()                    child.show_all()                    grandchild.show_all()                    # Try to find the newly added object                    for item in parent:                        print("trying to find another:", Gtk.Buildable.get_name(item))    except:        print(Gtk.Buildable.get_name(child))
查看完整描述

3 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

我不確定我可以給你一個適用于你現有代碼的例子,但我是這樣做的:


Figure = None

def invoice_chart_clicked (self, button):

        global Figure

        if Figure == None:

            from matplotlib.figure import Figure

            from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas

            from matplotlib.pyplot import pie

            self.figure = Figure(figsize=(4, 4), dpi=100)

            canvas = FigureCanvas(self.figure)  # a Gtk.DrawingArea

            canvas.set_size_request(800, 500)

            overlay = self.builder.get_object('overlay1')

            overlay.add (canvas)

        a = self.figure.add_subplot(111)

        labels = list()

        fractions = list()

        unpaid = 0

        self.cursor.execute("SELECT SUM(amount_due), c.name FROM invoices "

                            "JOIN contacts AS c ON c.id = invoices.customer_id "

                            "WHERE (canceled, paid, posted) = "

                            "(False, False, True) GROUP BY customer_id, c.name "

                            "ORDER BY SUM(amount_due)")

        for row in self.cursor.fetchall():

            customer_total = row[0]

            customer_name = row[1]

            fractions.append(customer_total)

            labels.append(customer_name)

            unpaid += 1

        if unpaid == 0:

            labels.append("None")

            fractions.append(1.00)

        a.pie(fractions, labels=labels, autopct='%1.f%%', radius=0.7)

        window = self.builder.get_object('window1')

        window.show_all()

每次我重新加載這個函數時,繪圖都會重新生成。您可以在此處找到完整代碼。我從來沒有運行過任何測試來查看所有內存是否都被正確釋放等等。也許它可以從那里開始。


查看完整回答
反對 回復 2021-09-14
  • 3 回答
  • 0 關注
  • 325 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號