我正在开发一个简单的 python GUI,用于使用 PyGObject/Gtk + Glade 来引导 gphoto2。我改编了 Gnome 教程,该教程使用生成器创建伪线程,允许将更新传递给 Gtk 主线程。一个子进程运行 gphoto2 并将命令传递给它以从 DSLR 相机拍摄一系列照片。我已经设法让程序在两次拍摄之间更新 GUI。但是,我计划做更长的镜头,实现一个取消生成器中间任务的选项会很有用。我将如何以最简单的方式实现这一点?
我使用了这个[教程]中的例子:https ://wiki.gnome.org/Projects/PyGObject/Threading
下面是正在编写的代码的简单示例。实际应用要大得多。
import gi
import re
import time
import subprocess
gi.require_version("Gtk", "3.0")
from gi.repository import Gio, GLib, Gtk, GObject
def app_main():
builder = Gtk.Builder()
builder.add_from_file("example.glade")
window = builder.get_object("GUI")
window.connect("destroy", Gtk.main_quit)
capture_button = builder.get_object("btn_capture")
def capture():
cmd = ['gphoto2', '--auto-detect']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = process.stdout.read().decode('utf-8')
process.wait()
usb_devices = re.findall('usb:001,' + '[0-9][0-9][0-9]', output)
working_directory = "/home/richard"
for i in range(1, 5):
new_port = '--port=' + usb_devices[0]
cmd = ['gphoto2', new_port, '--capture-image', '--keep']
process = subprocess.Popen(cmd, cwd=working_directory)
process.wait()
update_progress(f"Photo {i} of 5 taken!\n")
yield True
capture_button.set_sensitive(True)
def update_progress(text):
output = builder.get_object("txtview_console")
textviewbuffer = output.get_buffer()
start_iter = textviewbuffer.get_start_iter()
textviewbuffer.insert(start_iter, text)
return False
def on_capture_clicked(button):
capture_button.set_sensitive(False)
time.sleep(1)
run_generator(capture)
def on_cancel_clicked(button):
print("You pressed cancel!")
# I would like this to cancel the generator some how
呼唤远方
浮云间
相关分类