gpt4 book ai didi

python - 对话框中的 GTK 标签包装

转载 作者:太空狗 更新时间:2023-10-29 22:23:19 24 4
gpt4 key购买 nike

我正在尝试创建一个带有标签的不可调整大小的对话框。这个标签有很多文本,所以我希望它在不使对话框变宽的情况下换行

出于某种原因,我无法找出如何让 GTK 允许这种情况发生。我什至找不到在对话框上设置最大宽度的方法,这会很棒。

这是我的意思的运行示例:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from gi.repository import Gtk

class DialogExample(Gtk.Dialog):

def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))

self.set_default_size(150, 100)
self.set_resizable(False)

label = Gtk.Label("This is a dialog to display additional information, with a bunch of text in it just to make sure it will wrap enough for demonstration purposes")
label.set_line_wrap(True)

box = self.get_content_area()
box.add(label)
self.show_all()

class DialogWindow(Gtk.Window):

def __init__(self):
Gtk.Window.__init__(self, title="Dialog Example")

self.set_default_size(250, 200)


button = Gtk.Button("Open dialog")
button.connect("clicked", self.on_button_clicked)

self.add(button)

def on_button_clicked(self, widget):
dialog = DialogExample(self)
response = dialog.run()

if response == Gtk.ResponseType.OK:
print "The OK button was clicked"
elif response == Gtk.ResponseType.CANCEL:
print "The Cancel button was clicked"

dialog.destroy()

win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

最佳答案

我解决了这个问题(除了将换行设置为 True 之外)将 Gtk.Label 放在 Gtk.Table 中,使用 FILL 和 SHRINK 标志并为标签设置固定宽度。像这样:

label = Gtk.Label("This is a dialog to display additional information, with a bunch of text in it just to make sure it will wrap enough for demonstration purposes")
label.set_line_wrap(True)
label.set_size_request(250, -1) # 250 or whatever width you want. -1 to keep height automatic

table = Gtk.Table(1, 1, False)
table.attach(label, 0, 1, 0, 1, Gtk.AttachOptions.SHRINK | Gtk.AttachOptions.FILL)

这应该可以解决问题

关于python - 对话框中的 GTK 标签包装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12218970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com