使用python和GTK创建一个简单的选项卡式("multi page")应用程序



我希望有人能帮助我。

我的目标

  • 使用 python 创建一个简单的多页 GTK 应用程序
  • 应使用侧边栏或顶部栏切换页面
  • 每个页面应该能够包含多个元素(例如,几个按钮,标签等(排列在网格或其他东西中

到目前为止我的代码(一些从免费来源复制和粘贴和一些修改(

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class StackSidebar(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="application title")
        self.set_default_size(900, 600)
        self.connect("destroy", Gtk.main_quit)
        grid = Gtk.Grid()
        self.add(grid)
        stack = Gtk.Stack()
        stack.set_hexpand(True)
        stack.set_vexpand(True)
        grid.attach(stack, 1, 0, 1, 1)
        stacksidebar = Gtk.StackSidebar()
        stacksidebar.set_stack(stack)
        grid.attach(stacksidebar, 0, 0, 1, 1)
        label = Gtk.Label("label 1 text inside")
        name = "label1"
        title = "label 1 name"
        stack.add_titled(label, name, title)
        label = Gtk.Label("label 2 text inside")
        name = "label2"
        title = "label 2 name"
        stack.add_titled(label, name, title)
        label = Gtk.Label("label 3 text inside")
        name = "label3"
        title = "label 3 name"
        stack.add_titled(label, name, title)
window = StackSidebar()
window.set_wmclass ("application title", "application title")
window.show_all()
Gtk.main()

结果

单击链接以查看正在运行的应用程序

问题

我只能在每个页面中查看/创建一个标签。请参阅label = Gtk.Label("label 1 text inside") 。如前所述,我想安排一些按钮等等,但不知道如何开始。

你能帮忙吗?这可能吗?我的方法可以吗,还是应该使用GtkNotebook之类的东西?提前谢谢。

感谢您的帮助。我看了GtkGrid,它工作得很好。

这是我的解决方案(片段(

# See initial post for previous code
grid2 = Gtk.Grid()
button1 = Gtk.Button(label="Button 1")
label1 = Gtk.Label("This is a test label")
button3 = Gtk.Button(label="Button 3")
button4 = Gtk.Button(label="Button 4")
button5 = Gtk.Button(label="Button 5")
button6 = Gtk.Button(label="Button 6")
grid2.add(button1)
grid2.attach(label1, 1, 0, 2, 1)
grid2.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
grid2.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
grid2.attach(button5, 1, 2, 1, 1)
grid2.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)
name = "page3"
title = "page3-title"
stack.add_titled(grid2, name, title)
# See initial post for next code

结果

单击链接以查看正在运行的应用程序

相关内容

最新更新