【问题标题】:In Gtk, how to make a window smaller when creating在 Gtk 中,如何在创建时使窗口变小
【发布时间】:2021-02-13 12:49:42
【问题描述】:

我正在尝试显示带有条目小部件的图像和框。我可以这样做,但是窗口太大以至于底部的小部件大部分都看不见。我已经尝试了几个调用来设置窗口的大小或取消最大化它,但它们似乎没有效果。我确定该问题仅在图像很大时出现,但仍然想知道如何在可调整大小的窗口中显示大图像,或者就此而言,从代码中对窗口的几何形状进行任何更改。我尝试的所有函数调用似乎都没有效果。

这是我的代码:

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

from urllib.request import urlopen
class Display(object):

    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect('destroy', self.destroy)
        self.window.set_border_width(10)

        # a box underneath would be added every time you do 
        # vbox.pack_start(new_widget)

        vbox = Gtk.VBox()
        self.image = Gtk.Image()
        response = urlopen('http://1.bp.blogspot.com/-e-rzcjuCpk8/T3H-mSry7PI/AAAAAAAAOrc/Z3XrqSQNrSA/s1600/rubberDuck.jpg').read()

        pbuf = GdkPixbuf.PixbufLoader()
        pbuf.write(response)
        pbuf.close()
        self.image.set_from_pixbuf(pbuf.get_pixbuf())

        self.window.add(vbox)
        vbox.pack_start(self.image, False, False, 0)
        self.entry = Gtk.Entry()
        vbox.pack_start(self.entry, True,True, 0)

        self.image.show()
        self.window.show_all()

    def main(self):
        Gtk.main()

    def destroy(self, widget, data=None):
        Gtk.main_quit()

a=Display()
a.main()

【问题讨论】:

    标签: widget gtk3


    【解决方案1】:

    大部分贴出的信息似乎是关于 Gtk2 而不是 Gtk3,但有一个解决方案:使用 pix buf 加载器并设置大小:

    from gi.repository import Gtk, Gdk, GdkPixbuf
    
    #more stuff
    
    path = config['DEFAULT']['datasets']+'working.png'
    with open(path,'rb') as f:
        pixels = f.read()
    loader = GdkPixbuf.PixbufLoader()
    loader.set_size(400,400)
    loader.write(pixels)
    pb = GdkPixbuf.Pixbuf.new_from_file(path)
    self.main_image.set_from_pixbuf(loader.get_pixbuf())
    loader.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多