在matplot事件之后,gtk progressbar不再工作了吗?

时间:2013-04-19 09:46:34

标签: multithreading matplotlib gtk pygtk

嗨,我是python和gtk编程的新手 我正在编写一个程序来做一些测量。 为了绘制测量值,我使用了matplotlib 该程序将具有打开和关闭加热器并进行测量的功能 我想为加热器和测量使用单独的螺纹 目前,该程序尚未实现与硬件的通信。

问题是当我点击“测量按钮”时,“进度条”不再起作用。 我收到一条消息:

gtk.ProgressBar对象位于0x29b8460(未初始化为0x0)

当我只使用加热器按钮时,进度条会继续工作

我做错了什么?

这是代码

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
import time
import gobject
import threading
import matplotlib
import numpy as np
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas

class measure:
    # callback to quit
    def delete_event(self, widget, event, data = None):
        gtk.main_quit()
        return False

    def heater_helper(self, widget, heater, progressbar):
        print "starting heater thread"
        threading.Thread(target=self.heater_cb, args=(widget, heater, progressbar)).start()

    def heater_cb(self, widget, heater, progressbar):
        heaterstring = "6.3"
        heater = eval(heaterstring)
        stap = 1
        j = 0.1
        heatervalue = widget.get_active()
        print heatervalue
        progressbar.set_fraction(0.1)
        while (stap <= 10 ):
            if widget.get_active():
                print widget.get_active()
                fraction = j * stap
                print fraction
                progressbar.set_fraction(fraction)
                stap = stap + 1
                time.sleep(1)
            else:
                stap = 11
                progressbar.set_fraction(0.0)
                break

    def do_measurement_helper(self, widget, fig):
        print " Start measurement thread"
        threading.Thread(target=self.do_measurement, args=(widget, fig)).start()

    def do_measurement(self, widget, fig):
        fig.clear()
        ax = fig.add_subplot(111)
        x = np.arange(0, 5*np.pi, 0.01)
        y = np.sin(x**2)*np.exp(-x)
        ax.plot(x, y)
        fig.canvas.draw()

    def __init__(self):
        # Create new Window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.show()

        mainbox = gtk.HBox(False, 0)
        self.window.add(mainbox)
        mainbox.show()

        leftvbox = gtk.VBox(False, spacing = 10)
        mainbox.pack_start(leftvbox, expand = False, fill = False, padding = 0)
        leftvbox.show()

        rightvbox = gtk.VBox(False, spacing = 10)
        mainbox.pack_start(rightvbox, expand = False, fill = False, padding =0)
        rightvbox.show()

        heaterprogressbar = gtk.ProgressBar()
        leftvbox.pack_start(heaterprogressbar, expand = False, fill = False, padding = 0)
        heaterprogressbar.show()
        heaterbutton = gtk.ToggleButton("Heater")
        leftvbox.pack_start(heaterbutton, expand = True, fill = False, padding = 0)
        heaterbutton.show()
        heaterbutton.connect("toggled", self.heater_helper, heaterbutton, heaterprogressbar)

        fig = matplotlib.figure.Figure(figsize=(5,4), dpi=64)
        canvas = FigureCanvas(fig)
        rightvbox.pack_start(canvas, expand = True, fill = True, padding = 0 )
        canvas.show()
        measurebutton = gtk.Button("Measure")
        rightvbox.pack_start(measurebutton, expand = False, fill = False, padding = 0)
        measurebutton.show()
        measurebutton.connect("clicked", self.do_measurement_helper, fig)

def main():
    gtk.main()
    return(0)

if __name__ == "__main__":
    gtk.gdk.threads_init()
    measure()
    main()
    gtk.gdk.threads_leave()

亲切的问候,

Joris Weijters

2 个答案:

答案 0 :(得分:1)

可能不支持组合线程,Matplotlib和GTK主循环,并且难以准确调试正在发生的事情。我的建议是不要从线程进行任何GUI调用,而是使用gobject.idle_add()来安排它们。

答案 1 :(得分:0)

线程,Matplotlib和GTK主循环可以组合,如果你记住一些事情:

  1. 我使用gobject.threads_init()代替gtk.gdk.threads_init(),gtk.gdk变体与Matplotlib结合使用对我不起作用。我想你也可以省略gtk.gdk.threads_leave()
  2. 正如ptomato所提到的,你应该让主要的gtk线程通过调用gobject.idle_add()gobject.timeout_add()函数处理与gtk小部件有关的任何事情。 我通常会创建一个辅助函数来定期从float变量更新状态栏:

    def do_measurement(self):
        self.data = []
        self.progress = 0
        self.abort = threading.Event()
        gobject.timeout_add(100, self.update_progressbar)
        for point in some_generator_yielding_100_values():
            if self.abort.is_set():
                break
            self.data.append(point)
            self.progress += 0.01
        self.progress = None
    
    def update_progressbar(self):
        if self.progress is None:
            self.progressbar.set_fraction(0) # reset bar
            return False           # do not run again
        self.progressbar.set_fraction(self.progress)
        return True                # run again after 100ms
    
    def start_measurement(self):
        threading.Thread(target=self.do_measurement).start()
    
    def stop_measurement(self):
        self.abort.set()
    

    但您当然也可以致电gobject.idle_add(self.progressbar.set_fraction, x)以异步设置新值x