2017-06-12 76 views
1

我希望有人能帮助我。用Python和GTK创建一个简单的带标签(“多页”)应用程序

我的目标

  • 用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() 

结果

Click link to see the running application

问题

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

你能帮忙吗?这甚至有可能吗?我的方法好吗,还是应该使用类似GtkNotebook的东西?提前致谢。

+0

你需要把你的标签,按钮等为布局容器,例如[盒](http://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html#boxes)或表格。作为@ PM2Ring所说的 –

+0

,你需要使用容器小部件来布局你的UI。尝试Glade并使用小部件来了解如何制作用户界面。 –

+1

@ PM2Ring表格不再适用于GTK + 3;使用Grid代替。 – andlabs

回答

0

感谢您的帮助。我看了一下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 

结果

Click link to see running application

相关问题