创建在gtk中自动更新的小部件,同时仍然能够控制其他小部件

时间:2014-07-18 20:22:19

标签: python gtk python-multithreading glade

好的,在我的过程中我学习更多Python的一个项目中,我一直在尝试构建一个gtk应用程序来监控水温并以10秒的间隔更新gtk应用程序中的文本框。我还希望能够有一个倒计时器,可以在gtk中显示,同时每秒刷新一次。我有一个使用glade和gtk构建的GUI,但我遇到了应用程序锁定并变得无法响应的问题。经过一番阅读后,我发现我将不得不使用线程。我不知道我在做线程时做了什么,并认为也许有人可以帮助我。在使用gtk和Glade作为GUI构建器时,我真的很想看看如何使用线程。我发现这个代码使用gtk但我仍然遇到问题。我可以得到一些帮助吗?

import sys
import time
import gtk

from threading import Thread

threadcount=0

class Test (Thread):
    def __init__ (self,button, count=0):
        Thread.__init__(self)
        self.count = count
        self.button=button

    def run (self):
        for i in range(0,10):
            time.sleep(1)
            # Acquire and release the lock each time.
            gtk.threads_enter()
            self.button.set_label("Thread %002d - %d" % (self.count,i))
            gtk.threads_leave()
        gtk.threads_enter()
        self.button.set_label("  Start Thread  ")
        gtk.threads_leave()

def start_new_thread (button, data=None):
    global threadcount
    threadcount += 1
    a = Test(button,threadcount)
    a.start()


def hello(*args):
    """ Callback function that is attached to the button """
    print "Hello World"
    window.destroy()

def destroy(*args):
    """ Callback function that is activated when the program is destoyed
"""
    window.hide()
    gtk.main_quit()

# Initialize threads
gtk.threads_init()

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", destroy)
window.set_border_width(10)

button = gtk.Button("  Start Thread  ")
button.connect("clicked", start_new_thread,button)
window.add(button)
button.show()

window.show_all()
gtk.threads_enter()
gtk.main()
gtk.threads_leave()

这是我尝试使用的代码,它被简化为计时器功能。我不确定如何使一切工作。我读过一些关于gobjects的内容,但我对此知之甚少。

import gtk
import time
import sys
import os
import threading

TIME = 0
LOCK = threading.Lock()

class Timer(threading.Thread):
    def __init__(self, nMinutes):
        threading.Thread.__init__(self)
        self.nMin = nMin
    def run(self):
        global TIME
        nSeconds = self.nMinutes*60
        TIME = nSeconds
        startTime = time.time()
        while TIME != 0:
            ElapTime = time.time() - startTime
            LOCK.acquire()
            TIME = nSeconds - ElapTime
            LOCK.release()

class GUI_Timer_Update(threading.Thread):
    def __init__(self, TIME):
        threading.Thread.__init__(self)
        self.TIME = TIME
    def run(self):
        while TIME != 0:
            timeFormatted = self.format_time(TIME)
            LOCK.acquire()
            # Code that updates the GUI after formatting it
            LOCK.release()


    def format_time(self, x):
        minutes, seconds_rem = divmod(x, 60)
        if minutes >= 60:
            hours, minutes_rem = divmod(minutes, 60)
            return "%02d:%02d:%02d" % (hours, minutes_rem, seconds_rem)
        else:
            minutes, seconds_rem = divmod(x, 60)
            return "00:%02d:%02d" % (minutes, seconds_rem)

class GUI(object):
    def __init__(self):
        self.builder = gtk.Builder()
        self.builder.add_from_file("Timer.glade")
        self.window1 = self.builder.get_object("window1")
        self.window1.show()
        self.builder.connect_signals(self)

    def on_window1_destroy(self, object, data=None):
        print "quit on destroy"
        gtk.main_quit()

    def on_startTimer_clicked(self, widget):
        self.timer = self.builder.get_object("timer").get_text() # get time in minutes from timer text box 
        self.timer = int(self.timer) # convert the text to an int

    def on_timerReset_clicked(self, widget):
        self.builder.get_object("timer").set_text('')
        self.builder.get_object("TIME").set_text('')

    def main(self):
        gtk.main()  


if __name__ == "__main__":
    gui = GUI()
    gui.main()

我也想加入一个停止/暂停按钮。任何帮助,将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

好的,我已将我的代码编辑为以下内容:

import gtk, time, sys, os, gobject

COUNTER = 0
COUNTER2 = 0

class GUI(object):
    def __init__(self):
        self.builder = gtk.Builder()
        self.builder.add_from_file("Timer.glade")
        self.window1 = self.builder.get_object("window1")
        self.window1.show()
        self.builder.connect_signals(self)
        self.g = 0

    def on_window1_destroy(self, object, data=None):
        print "quit on destroy"
        gtk.main_quit()

    def on_startTimer_clicked(self, widget):
        global COUNTER
        self.time = self.builder.get_object("time").get_text()
        COUNTER = int(self.time)
        self.g = gobject.timeout_add(1000, self.timer)


    def on_startTimer2_clicked(self, widget):
        global COUNTER2
        self.time2 = self.builder.get_object("time2").get_text()
        COUNTER2 = int(self.time2)
        self.g2 = gobject.timeout_add(1000, self.timer2)

    def timer2(self):
        global COUNTER2
        while COUNTER2 >= 0:
            if COUNTER2 != 0:
                self.builder.get_object("time2").set_text(str(self.format_time(COUNTER2)))
                COUNTER2 -=1
                return True
            else:
                self.builder.get_object("time2").set_text("Finished")
                return False

    def timer(self):
        global COUNTER
        while COUNTER >= 0:
            if COUNTER != 0:
                self.builder.get_object("time").set_text(str(self.format_time(COUNTER)))
                COUNTER -=1
                return True
            else:
                self.builder.get_object("time").set_text("Finished")
                return False


    def format_time(self, x):
        minutes, seconds_rem = divmod(x, 60)
        if minutes >= 60:
            hours, minutes_rem = divmod(minutes, 60)
            return "%02d:%02d:%02d" % (hours, minutes_rem, seconds_rem)
        else:
            minutes, seconds_rem = divmod(x, 60)
            return "00:%02d:%02d" % (minutes, seconds_rem)



    def main(self):
        gtk.main()  


if __name__ == "__main__":
    gui = GUI()
    gui.main()

This video series让我开始了解如何使用gobject.timeout_add()。感谢user4815162342的建议。