好吧,在我学习更多 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()
我还想添加一个停止/暂停按钮。任何帮助,将不胜感激。谢谢