Gtk3 FileChooserDialog 高度异常与调整大小故障的排查及修复咨询
Gtk3 FileChooserDialog 高度异常与调整大小故障的排查及修复咨询
各位好,我在Ubuntu 24.04.3(GNOME Shell 46版本)上使用Gtk3的Gtk.FileChooserDialog组件时遇到了挺奇怪的问题,想请教下大家怎么解决:
- 高度异常:我明明设置了对话框的默认高度为500px,屏幕总高度是1080px,但实际弹出的对话框几乎占满了大半个屏幕,接近底部,和设置的高度完全不符。
- 调整大小故障:当我用鼠标手动拖拽调整对话框大小时,它会不受控制地缩到极小,还会卡在屏幕顶部,看起来像是只显示了某个子控件,而不是完整的对话框。
下面是我的测试代码、运行输出以及系统的GTK相关包信息:
测试代码
import sys import gi gi.require_version("Gtk", "3.0") from gi.repository import GLib, Gtk # noqa: E402 class Chooser(Gtk.FileChooserDialog): def __init__(self, title=None, parent=None): super().__init__( title=title, parent=parent, action=Gtk.FileChooserAction.OPEN, default_height=500, # 尝试初始化高度为500px ) self.set_default_size(-1, 500) # 尝试设置自动高度为500px self.set_size_request(-1, 300) # 尝试设置最小高度为300px self.add_buttons( Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK, ) print(f"{self.props.resizable=}") print(f"{self.props.resize_mode=}") print(f"{self.props.default_height=}") self.add_filters() self.run_self() def add_filters(self): filter_txt = Gtk.FileFilter() filter_txt.set_name(".txt files") filter_txt.add_mime_type("text") self.add_filter(filter_txt) def run_self(self): response = self.run() self.destroy() class App(Gtk.Application): def __init__(self): super().__init__(application_id="com.gnome.test") GLib.set_application_name("test") def do_activate(self): self.window = Gtk.ApplicationWindow(application=self, title="App") # 创建网格容器 self.grid = Gtk.Grid() self.window.add(self.grid) # 创建按钮 self.btn = Gtk.Button(label="From") # 布局按钮 self.grid.attach(self.btn, 0, 0, 1, 1) self.grid.set_row_homogeneous(True) self.grid.set_column_homogeneous(True) # 绑定点击事件 self.btn.connect("clicked", self.get_file) # 设置主窗口大小 self.window.set_default_size(width=500, height=34) # 显示所有控件 self.window.show_all() def get_file(self, widget): print("Chooser") chooser = Chooser(title="Chooser", parent=self.window) chooser.set_default_size(-1, 400) app = App() exit_status = app.run(sys.argv) sys.exit(exit_status)
VS Code运行输出
Chooser self.props.resizable=True self.props.resize_mode=<enum GTK_RESIZE_QUEUE of type Gtk.ResizeMode> self.props.default_height=500
系统GTK相关包信息
$ dpkg -l | grep GTK ii apport-gtk 2.28.1-0ubuntu3.8 all GTK+ frontend for the apport crash report system ii gir1.2-gnomebg-4.0:amd64 44.0-5build2 amd64 Introspection data for GnomeBG (GTK 4) ii gir1.2-gnomedesktop-3.0:amd64 44.0-5build2 amd64 Introspection data for GnomeDesktop (GTK 3) ii gir1.2-gnomedesktop-4.0:amd64 44.0-5build2 amd64 Introspection data for GnomeDesktop (GTK 4) ii gir1.2-gtk-3.0:amd64 3.24.41-4ubuntu1.3 amd64 GTK graphical user interface library -- gir bindings ii gir1.2-gtk-4.0:amd64 4.14.5+ds-0ubuntu0.4 amd64 GTK graphical user interface library -- gir bindings ii gir1.2-gtksource-5:amd64 5.12.0-1build1 amd64 gir files for the GTK+ syntax highlighting widget ii gir1.2-javascriptcoregtk-4.1:amd64 2.48.5-0ubuntu0.24.04.1 amd64 JavaScript engine library from WebKitGTK - GObject introspection data ii gir1.2-javascriptcoregtk-6.0:amd64 2.48.5-0ubuntu0.24.04.1 amd64 JavaScript engine library from WebKitGTK - GObject introspection data ii gir1.2-webkit-6.0:amd64 2.48.5-0ubuntu0.24.04.1 amd64 Web content engine library for GTK - GObject introspection data ii gir1.2-webkit2-4.1:amd64 2.48.5-0ubuntu0.24.04.1 amd64 Web content engine library for GTK - GObject introspection data ii gnome-accessibility-themes 3.28-2ubuntu5 all High Contrast GTK 2 theme and icons ii gnome-themes-extra:amd64 3.28-2ubuntu5 amd64 Adwaita GTK 2 theme — engine ii gnome-themes-extra-data 3.28-2ubuntu5 all Adwaita GTK 2 theme and Adwaita-dark GTK 3 theme — common files ii gstreamer1.0-gtk3:amd64 1.24.2-1ubuntu1.1 amd64 GStreamer plugin for GTK+3 ii gtk2-engines-pixbuf:amd64 2.24.33-4ubuntu1.1 amd64 pixbuf-based theme for GTK 2 ii ibus-gtk:amd64 1.5.29-2 amd64 Intelligent Input Bus - GTK2 support ii ibus-gtk3:amd64 1.5.29-2 amd64 Intelligent Input Bus - GTK3 support ii ibus-gtk4:amd64 1.5.29-2 amd64 Intelligent Input Bus - GTK4 support ii libadwaita-1-0:amd64 1.5.0-1ubuntu2 amd64 Library with GTK widgets for mobile phones ii libavahi-ui-gtk3-0:amd64 0.8-13ubuntu6 amd64 Avahi GTK+ User interface library for GTK3 ii libayatana-appindicator3-1 0.5.93-1build3 amd64 Ayatana Application Indicators (GTK-3+ version) ii libayatana-indicator3-7:amd64 0.9.4-1build1 amd64 panel indicator applet - shared library (GTK-3+ variant)
我尝试过在初始化、实例化后多次设置高度和最小高度,但都没效果,想问问大家我哪里操作错了?有没有办法修复这些问题?
内容来源于stack exchange
相关产品推荐
相关产品推荐

