Gtk.PrintOperation.run()没有响应

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gtk.PrintOperation.run()没有响应相关的知识,希望对你有一定的参考价值。

我试图使用以下脚本来打印文档。问题是没有任何反应。打印“执行”一词,当我点击对话框取消按钮时,响应符合预期。 PREVIEW和PRINT按钮使对话框退出但不打印结果。三个回调方法都被删除,每个方法都打印出它的名字,其中没有一个显示出来。

我使用的是Ubuntu 18.04和python 3.6.7。

import gi
gi.require_version ('Gtk', '3.0')
from gi.repository import Gtk, GLib, Gdk

def PrintManager (self):
    ps = Gtk.PrintSettings.new ()
    po = Gtk.PrintOperation.new ()
    po.set_print_settings (ps)
    po.set_n_pages (1)

    po.connect ("begin_print", self.BeginPrint, self.PrintList)
    po.connect ("draw_page", self.DrawPage, self.PrintList)
    po.connect ("end_print", self.EndPrint, self.PrintList)

    print ("execute")                                                     #x
    result = po.run (Gtk.PrintOperationAction.PRINT_DIALOG, const.internal["parent"])
    print (result)                                                        #x

    if result == Gtk.PrintOperationResult.APPLY:
       ps = op.get_print_settings ()

    elif result == Gtk.PrintOperationResult.ERROR:
       dialog = Gtk.MessageDialog.new (Gtk.DialogFlags.DESTROY_WITH_PARENT,
          Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, const.printError)
       dialog.run ()
       dialog.destroy ()


def BeginPrint (self, po, ctx, data):
    print ("{}.BeginPrint ()".format (__name__)) 


def DrawPage (self, po, ctx, cnt, data):
    print ("{}.DrawPage ({})".format (__name__, cnt))


def EndPrint (self, po, ctx, data):
    print ("{}.EndPrint ()".format (__name__))
答案

您的程序有问题(很可能是const的事情),因为这确实按预期工作:

#!/usr/bin/env python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


class GUI:
    def __init__(self):

        self.window = Gtk.Window()
        button = Gtk.Button(label = "print op")
        button.connect("clicked", self.button_clicked )
        self.window.add(button)
        self.window.set_size_request(400, 400)
        self.window.show_all()

    def button_clicked (self, button):
        ps = Gtk.PrintSettings.new ()
        po = Gtk.PrintOperation.new ()
        po.set_print_settings (ps)
        po.set_n_pages (1)
        self.PrintList = []
        po.connect ("begin_print", self.BeginPrint, self.PrintList)
        po.connect ("draw_page", self.DrawPage, self.PrintList)
        po.connect ("end_print", self.EndPrint, self.PrintList)

        print ("execute")                                                     #x
        result = po.run (Gtk.PrintOperationAction.PRINT_DIALOG, self.window)
        print (result)                                                        #x

        if result == Gtk.PrintOperationResult.APPLY:
            ps = po.get_print_settings ()

        elif result == Gtk.PrintOperationResult.ERROR:
            dialog = Gtk.MessageDialog.new (Gtk.DialogFlags.DESTROY_WITH_PARENT,
                Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, po.printError)
            dialog.run ()
            dialog.destroy ()


    def BeginPrint (self, po, ctx, data):
        print ("{}.BeginPrint ()".format (__name__)) 


    def DrawPage (self, po, ctx, cnt, data):
        print ("{}.DrawPage ({})".format (__name__, cnt))


    def EndPrint (self, po, ctx, data):
        print ("{}.EndPrint ()".format (__name__))

    def on_window_destroy(self, window):
        Gtk.main_quit()

app = GUI()
Gtk.main()

以上是关于Gtk.PrintOperation.run()没有响应的主要内容,如果未能解决你的问题,请参考以下文章

为啥我SecureCRT点连接没反应?

为啥我的网页上jQuery没反应

笔记本突然没声音了如何恢复 笔记本突然没声音解决方法

Win10开始菜单右键没反应怎么办 菜单右键没反应解决办法

鼠标点击提交后网页没反应

网页点了没反应,怎么回事?