2017-03-01 35 views
0

我目前在Python上使用gtk来创建一些图形界面。我正在努力解决一个小问题:我想在笔记本的几个页面上显示一个gtk.widget(例如HBox或Button),但我无法成功。小部件只显示在使用它的第一页上,但从未显示在下面的页面上。我已经尝试了重新设置方法,但是出现了问题,小部件只显示在最后一个。Python Gtk:如何在同一个笔记本的不同页面上使用相同的小部件?

import gtk 
w = gtk.Window(gtk.WINDOW_TOPLEVEL) 
w.connect("destroy", gtk.main_quit) 
w.set_title("Some tests") 
legnth = 850 
width = 700 
w.set_size_request(length, width) 

VBoxWindow = gtk.HBox() 

hbox1 = gtk.HBox() 
notebook1 = gtk.Notebook() 
page1 = gtk.Notebook() 
page12 = gtk.VBox() 
page13 = gtk.VBox() 
page14 = gtk.VBox() 

page1.append_page(page12, gtk.Label(' PAGE1 ') 
page1.append_page(page13, gtk.Label(' PAGE2 ') 
page1.append_page(page14, gtk.Label(' PAGE3 ') 

box1 = gtk.VBox() 
button1 = gtk.Button("BTN 1") 
box1.pack_start(button1, True, True, 5) 
page12.pack_start(box1, False, False, 5) 

box2 = gtk.VBox() 
box2.pack_start(button1, True, True, 5) 
page13.pack_start(box2, False, False, 5) 

# reparenting method test 
#box3 = gtk.VBox() 
#button1.reparent(box3) 
#box3.pack_start(button1, True, True, 5) 
#page13.pack_start(box3, False, False, 5) 

page1.props.border_width = 12 
page1.add(page12) 
page1.add(page13) 
notebook1.append_page(page1, gtk.Label('Some MAIN Page')) 
VBoxWindow.add(notebook1) 
w.add(VBoxWindow) 
displayReady = True 
w.show_all() 
gtk.main() 
+0

请添加你迄今为止所做的。如果不对,请不要留意。所有我需要的是你想要做什么的概述。 –

+0

@ChihebNexus是的,这里是我要做的代码的精神 –

+0

@ G.Esel你究竟做了什么?为什么它不能在你的情况下创建重复的小部件?更少的代码可能? – theGtknerd

回答

1

很抱歉的等待,但我完全忘记与你们分享我的解决方案!

这里是我的发现:

def reparent_on_switch(self, widget, page): 
    for p in range(widget.get_n_pages()): 
     container = widget.get_nth_pages(p) 
     if not (p == page): 
      pass 
     else: 
      self.foo.reparent(container) 
      container.pack_start(self.foo) 

它的工作原理就像一个魅力!

0

您可以按照内pyGObjectpython3本教程以解决您的问题。

尝试,按照每一个步骤:

# This is a test application within PyGObject version 3.0+ 
import gi 
# Test if gi version is 3.0+ 
gi.require_version('Gtk', '3.0') 

from gi.repository import Gtk 

class TestNoteBook(Gtk.Window): 
    def __init__(self): 
     # Initialize Gtk.Window 
     Gtk.Window.__init__(self) 
     # Add title to this app 
     self.set_title("Test Note Book") 
     # Connect the close button to self.quit_app() method 
     self.connect("delete-event", self.quit_app) 

     # Create a new Gtk.Widget with type Gtk.Notebook() 
     notebook = Gtk.Notebook() 
     # Add notebook to the current app class 
     self.add(notebook) 
     # Create a Gtk.HBox() in which we can add widgets 
     page1 = Gtk.HBox() 
     # Create button1 
     button1 = Gtk.Button("I'm in the page 1") 
     # Connect button1 to a signal 
     button1.connect("clicked", self.button_clicked, "button1") 
     # Add a Button to page1 
     page1.add(button1) 
     # Add page1 to the notebook with label page-1 
     notebook.append_page(page1, Gtk.Label("page-1")) 
     # Create new Gtk.HBox() 
     page2 = Gtk.HBox() 
     # Create button2 
     button2 = Gtk.Button("I'm in the page 2") 
     # Add a signal to button2 
     button2.connect("clicked", self.button_clicked, "button2") 
     # Add a button to page2 
     page2.add(button2) 
     # Add pag2e to the notebook with label page-2 
     notebook.append_page(page2, Gtk.Label("page-2")) 

    def run(self): 
     # Show all the widgets 
     self.show_all() 
     # Run the Gtk.main loop 
     Gtk.main() 

    def quit_app(self, widget, args): 
     # Print a message with the ID of the widget and its argument 
     print("Exit this current window...", widget, args) 
     # Exit from the window 
     Gtk.main_quit() 

    def button_clicked(self, widget, args): 
     if args == "button1": 
      print("Button 1 clicked!") 
     else: 
      print("Button2 clicked!") 


# test 
if __name__ == '__main__': 
    app = TestNoteBook() 
    app.run() 
+0

是的,但在这个例子中,你创建了2个不同的按钮链接在同一个信号上,但主要问题是我想在笔记本的两个页面上都显示一个按钮。我的问题是要找到一种方法,或者在笔记本的不同页面上同时拥有完全相同的按钮(或小部件)(就好像它存在于两个页面上一样),或者像切换独特按钮的方式当前使用的笔记本页面(有两个页面上有相同按钮的印象) –

+0

您不能。小部件必须只有一个父母不多。 –

+0

我刚刚阅读了您推荐的教程,但是有一种方法可以根据笔记本中使用的当前页面来重新部署一个小部件。你知道有什么技术吗? –

1

号,窗口小部件只能有一个家长在同一时间。如果您想要在选项卡的所有页面上显示相同的控件,则必须创建重复控件,在选项卡页面更改时动态移动它们,或将它们完全放在选项卡控件之外。

移动控件是通过调用旧父项上的remove()和新父项上的add()(或特定于控件的变体,如push_start())完成的。 (我忘了,如果你需要管理Python的引用计数,如果是的话,你还必须ref()小部件移动第一和unref()后来,否则remove()会破坏小部件。)

+0

好的,谢谢!那么如何检测页面的变化呢?我对gtk比较陌生,你的技术的一个例子很可爱。 –

相关问题