在PyGtk3中调整Gtk.DrawingArea的大小

时间:2019-11-03 22:31:53

标签: python-3.x gtk3 pycairo drawingarea

请运行以下代码以获取如下内容:

enter image description here

基本上,我已经建立了一个Gtk.DrawingArea,它位于一个Gtk.Viewport内部,它也位于一个Gtk.Scrolledwindow中。在上述的DrawingArea中,我绘制图像,并使用2个按钮可以缩放该图像。

# -*- encoding: utf-8 -*-

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


class MyWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title="DrawingTool")
        self.set_default_size(800, 600)

        # The ratio
        self.ratio = 1.

        # Image
        filename = "image.jpg"
        self.original_image = GdkPixbuf.Pixbuf.new_from_file(filename)
        self.displayed_image = GdkPixbuf.Pixbuf.new_from_file(filename)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        # Zoom buttons
        self.button_zoom_in = Gtk.Button(label="Zoom-In")
        self.button_zoom_out = Gtk.Button(label="Zoom-Out")      
        # |ScrolledWindow
        # |-> Viewport
        # |--> DrawingArea 
        scrolledwindow = Gtk.ScrolledWindow()
        viewport = Gtk.Viewport()
        self.drawing_area = Gtk.DrawingArea()
        self.drawing_area.set_size_request(
                              self.displayed_image.get_width(), self.displayed_image.get_height())
        self.drawing_area.set_events(Gdk.EventMask.ALL_EVENTS_MASK)

        # Pack
        viewport.add(self.drawing_area)
        scrolledwindow.add(viewport)
        box.pack_start(self.button_zoom_in, False, True, 0)
        box.pack_start(self.button_zoom_out, False, True, 0)
        box.pack_start(scrolledwindow, True, True, 0)
        self.add(box)

        # Connect
        self.connect("destroy", Gtk.main_quit)
        self.button_zoom_in.connect("clicked", self.on_button_zoom_in_clicked)
        self.button_zoom_out.connect("clicked", self.on_button_zoom_out_clicked)
        self.drawing_area.connect("enter-notify-event", self.on_drawing_area_mouse_enter)
        self.drawing_area.connect("leave-notify-event", self.on_drawing_area_mouse_leave)
        self.drawing_area.connect("motion-notify-event", self.on_drawing_area_mouse_motion)
        self.drawing_area.connect("draw", self.on_drawing_area_draw)

        self.show_all()

    def on_button_zoom_in_clicked(self, widget):
        self.ratio += 0.1
        self.scale_image()
        self.drawing_area.queue_draw()

    def on_button_zoom_out_clicked(self, widget):
        self.ratio -= 0.1
        self.scale_image()
        self.drawing_area.queue_draw()

    def scale_image(self):
        self.displayed_image = self.original_image.scale_simple(self.original_image.get_width() * self.ratio, 
                                   self.original_image.get_height() * self.ratio, 2)

    def on_drawing_area_draw(self, drawable, cairo_context):
        pixbuf = self.displayed_image           
        self.drawing_area.set_size_request(pixbuf.get_width(), pixbuf.get_height())
        Gdk.cairo_set_source_pixbuf(cairo_context, pixbuf, 0, 0)
        cairo_context.paint()

    def on_drawing_area_mouse_enter(self, widget, event):
        print("In - DrawingArea")

    def on_drawing_area_mouse_leave(self, widget, event):
        print("Out - DrawingArea")

    def on_drawing_area_mouse_motion(self, widget, event):

        (x, y) = int(event.x), int(event.y)
        offset = ( (y*self.displayed_image.get_rowstride()) + 
                   (x*self.displayed_image.get_n_channels()) )
        pixel_intensity = self.displayed_image.get_pixels()[offset]
        print("(" + str(x) + ", " + str(y) + ") = " + str(pixel_intensity))



MyWindow()
Gtk.main()

运行该应用程序。我还设置了一些回调,因此,当您将鼠标指针悬停在图像上时,您会知道:

i)当您输入DrawingArea

ii)离开绘图区时

iii)在DrawingArea处的(x,y)和图像像素强度

我面临的问题是,当您缩小足够多的时间时,图像将如下所示:

enter image description here

但是,“鼠标进入”和“鼠标离开”信号以及“鼠标运动”信号都被发送,就像DrawingArea仍具有创建时的尺寸一样。请将鼠标悬停在图像上方和图像外部但在DrawingArea内部,以查看终端中的输出。

我希望将信号悬停在图像之外时不要发送自己。也就是说,如果可能,请将DrawingArea大小调整为显示的图像大小。

1 个答案:

答案 0 :(得分:1)

我使用了gtk_window_resize(),如此处所述:Cannot reduce size of gtk window programatically

def on_drawing_area_draw(self, drawable, cairo_context):
        pixbuf = self.displayed_image
        # New line. Get DrawingArea's window and resize it.
        drawable.get_window().resize(pixbuf.get_width(), pixbuf.get_height())           
        drawable.set_size_request(pixbuf.get_width(), pixbuf.get_height())
        Gdk.cairo_set_source_pixbuf(cairo_context, pixbuf, 0, 0)
        cairo_context.paint()