我正在尝试使用水平框将带有标签的条目小部件放在一行中。但是,标签会垂直扩展。它采用窗口的初始高度,并且不允许在其下方缩小。我尝试了填充,扩展,对齐等所有可能的组合,但是并没有改变。
如何解决?
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
self.init_ui()
def init_ui(self):
hbox = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6)
hbox.set_homogeneous(True)
hbox.set_baseline_position(Gtk.BaselinePosition.CENTER)
entry = Gtk.Entry()
entry.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(6400, 6400, 6440))
entry.connect("key-release-event", self.on_key_release)
hbox.pack_start(entry, False, False, 10)
self.label = Gtk.Label("Hello")
self.label.set_width_chars(15)
self.label.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(40000, 40000, 40000))
hbox.pack_start(self.label, False, False, 10)
self.add(hbox)
self.set_title("Entry")
self.set_size_request(350, 250)
self.connect("destroy", Gtk.main_quit)
self.show_all()
def on_key_release(self, widget, event):
self.label.set_text(widget.get_text())
win = MyWindow()
Gtk.main()
答案 0 :(得分:2)
在水平框之前添加另一个垂直框:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
self.init_ui()
def init_ui(self):
hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, spacing = 6)
hbox.set_homogeneous(True)
hbox.set_baseline_position(Gtk.BaselinePosition.CENTER)
entry = Gtk.Entry()
entry.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(6400, 6400, 6440))
entry.connect("key-release-event", self.on_key_release)
hbox.pack_start(entry, False, False, 10)
self.label = Gtk.Label("Hello")
self.label.set_width_chars(15)
self.label.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(40000, 40000, 40000))
hbox.pack_start(self.label, False, False, 10)
vbox = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
vbox.pack_start(hbox, False, False, 0)
self.add(vbox)
self.set_title("Entry")
self.set_size_request(350, 250)
self.connect("destroy", Gtk.main_quit)
self.show_all()
def on_key_release(self, widget, event):
self.label.set_text(widget.get_text())
win = MyWindow()
Gtk.main()
请注意,在Python3和Gtk中使用非关键字参数并不总是能正常工作,请使用hbox = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6)
代替hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, spacing = 6)