列举一个gtk3 GtkBox例子
时间: 2024-05-28 16:03:38 浏览: 30
以下是一个使用GtkBox布局的gtk3例子:
```python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class BoxExample(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Box Example")
self.set_border_width(10)
# 创建一个纵向的GtkBox
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
# 创建一个横向的GtkBox
hbox = Gtk.Box(spacing=6)
vbox.pack_start(hbox, True, True, 0)
# 添加一个按钮到横向GtkBox中
button = Gtk.Button.new_with_label("Button 1")
hbox.pack_start(button, True, True, 0)
# 添加第二个按钮到横向GtkBox中
button = Gtk.Button.new_with_label("Button 2")
hbox.pack_start(button, True, True, 0)
# 创建一个分隔线
separator = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
vbox.pack_start(separator, True, True, 0)
# 添加一个标签到纵向GtkBox中
label = Gtk.Label()
label.set_markup("<big>Label</big>")
vbox.pack_start(label, True, True, 0)
# 添加第二个分隔线
separator = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
vbox.pack_start(separator, True, True, 0)
# 添加一个文本框到纵向GtkBox中
entry = Gtk.Entry()
vbox.pack_start(entry, True, True, 0)
self.add(vbox)
win = BoxExample()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
```
该示例创建了一个包含两个按钮、一个标签和一个文本框的GtkBox。其中,按钮被包含在横向的GtkBox中,而标签和文本框则被包含在纵向的GtkBox中。可以使用`pack_start()`方法将组件添加到GtkBox中,并指定是否让它们填充可用空间以及是否将它们视为可扩展的。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)