2017-06-12 73 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

相關問題