如何使用Glib.timeout_add()函数等待按键事件

时间:2018-02-06 15:51:22

标签: python-3.x gtk3 glib

我正在尝试创建一个测试应用程序。应用程序应将图像列在目录中,在屏幕上显示第一个图像,获取键盘响应并显示第二个图像,进行响应,并为每个图像执行此操作。

我创建了一个glade文件,我正在使用它在RavensAPM类中创建接口。林间空地文件中有一个Gtk.Window,其中包含一个Gtk.Box,而Gtk.Image又包含一个run_ravens_apm()。我使用RavensAPM函数列出图像并将图像逐个送到RavensAPM类。在show_all()课程中,我更新图片并使用GLib.timeout_add()呈现所有内容。

此时我希望应用程序等待按键[1-8]和键盘按键[1-8]的按键事件。

我检查了for函数的用法,但我无法从中获得所需的行为。目前,run_ravens_apm()内的RavensAPM循环同时调用GLib.timeout_add()并创建与图像数量一样多的窗口,而无需等待键盘信号/事件。

我尝试将__call__()放在类的class RavensAPM: def __init__(self, test_image): self.builder = Gtk.Builder() self.builder.add_from_file('glade/window_ravens_apm.glade') self.window = self.builder.get_object('window_ravens_apm') self.window.fullscreen() self.window.connect('key-release-event', self.on_event_after) window_height = 1080 self.pixbuf_file = \ GdkPixbuf.Pixbuf.new_from_file_at_scale(test_image, -1, window_height, True) self.image = self.builder.get_object('ravens_apm_image') self.image.set_from_pixbuf(self.pixbuf_file) self.window.show_all() def on_event_after(self, widget, event): keys = [i for i in range(49, 57)] + [i for i in range(65457, 65465)] key_value = event.keyval if keys in key_value: self.window.destroy() return False def __call__(self): self.on_event_after() return True def run_ravens_apm(): # List the contents of img/ravens_apm # Use a loop to run RavensAPM for the length of images interval_ms = 200 img_directory = 'img/ravens_apm' img_files = os.listdir(img_directory) img_files.sort() img_files = img_files[:2] nr_images = len(img_files) for i in range(nr_images): img_file = os.path.join(img_directory, img_files[i]) ravens = RavensAPM(img_file) GLib.timeout_add(interval_ms, ravens) print('GLib.timeout_add must wait for Gdk.EventKey event') print(datetime.datetime.now()) 方法中,并将该类的实例用作函数,但它也不起作用。

我做错了什么?

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window_ravens_apm">
    <property name="can_focus">False</property>
    <signal name="key-press-event" handler="on_windows_ravens_apm_key_press_event" swapped="no"/>
    <child>
      <object class="GtkViewport" id="viewport1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkBox" id="box1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkImage" id="ravens_apm_image">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="stock">gtk-missing-image</property>
                <signal name="key-press-event" handler="on_ravens_apm_keypress" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

林间空地文件的内容如下。

$number = (int) $_GET['tics'];
$items = range(1, $number);

1 个答案:

答案 0 :(得分:0)

如果您需要更长的答案,我会在此处发布我的解决方案。很简单。现在我知道我应该从未使用过Glib.timeout_add()函数。

#!/usr/bin/python3
import gi
import os
import random
from gi.repository import Gtk
from gi.repository import GdkPixbuf
# from gi.repository import Gdk
# from gi.repository import GLib
gi.require_version('Gtk', '3.0')


FAKING_FIRTS = None
WINDOW_HEIGHT = 1080


class RavensAPM:
    def __init__(self, img_list, window_height):
        self.img_list = img_list
        self.window_height = window_height

        self.builder = Gtk.Builder()
        self.builder.add_from_file('glade/window_ravens_apm.glade')

        self.window = self.builder.get_object('window_ravens_apm')
        self.window.connect('key-release-event', self.on_key_released)
        self.window.fullscreen()

        self.ravens_image = self.builder.get_object('ravens_apm_image')
        self.pixbuf_file = \
            GdkPixbuf.Pixbuf.new_from_file_at_scale(self.img_list[-1],
                                                    -1,
                                                    self.window_height,
                                                    True)
        self.ravens_image.set_from_pixbuf(self.pixbuf_file)

        self.window.show_all()

    def on_key_released(self, widget, event):
        key_value = event.keyval
        keys_list = list(range(49, 57)) + list(range(65429, 65436))
        if key_value in keys_list:
            # TODO
            # Do stg to save the keyvalue somewhere
            self.img_list.pop()
            self.pixbuf_file = \
                GdkPixbuf.Pixbuf.new_from_file_at_scale(self.img_list[-1],
                                                        -1,
                                                        self.window_height,
                                                        True)
            self.ravens_image = self.builder.get_object('ravens_apm_image')
            self.ravens_image.set_from_pixbuf(self.pixbuf_file)


def run_ravens_apm():
    img_directory = 'img/ravens_apm'
    img_files = os.listdir(img_directory)
    img_files.sort(reverse=True)
    img_files = [os.path.join(img_directory, i) for i in img_files]
    RavensAPM(img_files, WINDOW_HEIGHT)

这是我使用的Glade文件。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window_ravens_apm">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkViewport" id="viewport1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkBox" id="box1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkImage" id="ravens_apm_image">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="stock">gtk-missing-image</property>
                <signal name="key-press-event" handler="on_ravens_apm_keypress" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>