我想獲得一個最小的實現Python3/GTK3的剪貼板工作。它會工作,當且僅當我運行Gtk.main()
。我甚至嘗試過:最低實現Gtk.Clipboard不起作用
While Gtk.event_pending():
Gtk.main_iteration()
沒有它的工作。
如果我運行Gtk.main()
它的行爲與預期讓我文成測試GUI編輯器粘貼腳本。該腳本將一個文本字符串t
放入剪貼板。當且僅當調用Gtk.main()
時,字符串文本將在那裏。如果它沒有被調用,但是剪貼板緩衝區被清除,但是是空的。我真的很感謝在這方面的幫助。任何想法如何讓它工作,而不致電Gtk.main()
?
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from gi.repository import Gtk
from gi.repository import Gdk
import signal
class Chars(Gtk.Window):
def __init__(self):
super().__init__()
self.connect('destroy', Gtk.main_quit)
self.show_all()
self.cb = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
t='Some text'
self.cb.set_text(t, -1)
while Gtk.events_pending():
Gtk.main_iteration()
def main():
chars = Chars()
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Gtk.main() # works if this is uncommented
if __name__ == "__main__":
main()
問候, Narnie