简体   繁体   English

Python Gtk 笔记本通过按钮打开新标签

[英]Python Gtk Notebook making a new tab open from a button

I'm pretty new to OOP and gtk programming, so sorry if the answer my question is really obvious, but I can't find a solution.我对 OOP 和 gtk 编程很陌生,很抱歉,如果我的问题的答案真的很明显,但我找不到解决方案。 I am trying to make a browser-like interface using the Gtk notebook.我正在尝试使用 Gtk 笔记本制作类似浏览器的界面。 I wrote a method to add tabs, and it seems to work, becasue when I call it in the init , it works, and adds a new tab.我写了一个添加选项卡的方法,它似乎可以工作,因为当我在init中调用它时,它可以工作,并添加一个新选项卡。 Here the method is:这里的方法是:

def create_page(self, button):
        print("creating a new page")
        print(self)
        self.newpage = Gtk.Box()
        self.newpage.set_border_width(50)
        self.newpage.add(Gtk.Label.new("add notes here"))
        self.notebook.append_page(self.newpage, Gtk.Label.new("new page")) 

The reason the method has to have the button parameter is becasue I want it to be called by a button, and for that to happen, it has to have a button parameter.该方法必须具有按钮参数的原因是因为我希望它被按钮调用,并且为此,它必须具有按钮参数。

When the button calls the parameter, the print statment works, and it prints its self < main .MyWindow object at 0x7efd64e52a80 ( main +MyWindow at 0xe60270)>.当按钮调用参数时,打印语句起作用,它打印自己的 < main .MyWindow object at 0x7efd64e52a80 ( main +MyWindow at 0xe60270)>。 It prints the exact same output as when I call it from the init .The problem is that it never actually adds the new notebook tab for some reason.它打印的 output 与我从init调用它时完全相同。问题是由于某种原因它实际上从未添加新的笔记本选项卡。 Here my full code is:这里我的完整代码是:

import gi 
# Since a system can have multiple versions 
# of GTK + installed, we want to make  
# sure that we are importing GTK + 3. 
gi.require_version("Gtk", "3.0") 
from gi.repository import Gtk 
  
  
class MyWindow(Gtk.Window): 
    def __init__(self): 
        Gtk.Window.__init__(self, title ="Stickies Hub") 
        #self.set_border_width(70) 
  
        # Create Notebook 
        self.notebook = Gtk.Notebook.new() 
        self.add(self.notebook) 
        #create buttons
        self.new_tab = Gtk.Button(label=("button"))
        self.new_tab.connect("clicked", self.create_page)
        # Create pages
        self.page1 = Gtk.Box()
        self.page1.set_border_width(50) 
        self.page1.add(Gtk.Label.new("Welcome to Geeks for Geeks")) 
        self.notebook.append_page(self.page1, Gtk.Label.new("Click Here")) 
  
        self.page2 = Gtk.Box() 
        self.page2.set_border_width(50) 
        self.page2.add(Gtk.Label.new("A computer science portal for geeks"))
        self.page2.add(self.new_tab) 
        self.notebook.append_page(self.page2, Gtk.Label.new("Click Here"))
        self.create_page(self.new_tab)
        self.create_page(self.new_tab)
        

    def create_page(self, button):
        print("creating a new page")
        print(self)
        self.newpage = Gtk.Box()
        self.newpage.set_border_width(50)
        self.newpage.add(Gtk.Label.new("new page"))
        self.notebook.append_page(self.newpage, Gtk.Label.new("new page")) 
  
  
win = MyWindow() 
win.connect("destroy", Gtk.main_quit) 
# Display the window. 
win.show_all() 
# Start the GTK + processing loop 
Gtk.main() 

How can I add a new notebook tab from a button?如何从按钮添加新的笔记本选项卡? Thanks so much for help!非常感谢您的帮助!

As jackw11111 said, the solution was to add self.show_all() at the end of create_page function.正如jackw11111所说,解决方案是在create_page function的末尾添加self.show_all()。 Thanks so much!非常感谢!

I made an answer so anyone with this same problem could easily find the answer.我做了一个答案,所以任何有同样问题的人都可以很容易地找到答案。

Helloo, Here is my code.你好,这是我的代码。 I think this will work for you.我认为这对你有用。

from gi.repository import Gdk
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    notebook = Gtk.Notebook()

    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(750, 500)
        self.connect("destroy", Gtk.main_quit)
        self.list_view()
        
    def list_view(self):
        self.table = Gtk.Table(n_rows=3, n_columns=3, homogeneous=True)
        listbox = Gtk.ListBox()
        self.add(self.table)
        self.add(listbox)

        self.two_d_array = {'Hello' : 'Hi', 'Example' : 'Merhaba'}
        for i in self.two_d_array.keys():
            ## label yerine buton oluşturduk
            items = Gtk.Button.new_with_label(i)
            items.connect("button-press-event",self.button_clicked)
            listbox.add(items)
        self.table.attach(listbox,0,1,0,3)
        
        self.add(self.notebook)
        self.table.attach(self.notebook,1,3,0,3)

        self.notebook.show_all()
        self.page1 = Gtk.Box()
        self.page1.set_border_width(10)
        self.page1.add(Gtk.Label(label="Merhaba bu ilk sayfa."))
        self.notebook.append_page(self.page1, Gtk.Label(label="Default Page"))

    def context_menu(self):
        menu = Gtk.Menu()
        menu_item = Gtk.MenuItem("New Page")
        menu.append(menu_item)
        menu_item.connect("activate", self.on_click_popup)
        menu.show_all()

        return menu

    ##  Buton sağ click ise context menu açtı
    def button_clicked(self,listbox_widget,event): 
        if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
            menu = self.context_menu()
            ## Tıklanan objenin labelini print ediyor
            print(listbox_widget.get_label())
            self.labelmenu = listbox_widget.get_label()
            menu.popup( None, None, None,None, event.button, event.get_time()) 
            return True

    def on_pop_menu(self, widget, event):
        if event.button == 3:
            widget.popup(None, None, None, None, event.button, event.time)
        
    def on_click_popup(self, action):   
        ## Yeni sayfa oluştur     
        self.new_page = Gtk.Box()
        self.new_page.set_border_width(10)
        self.new_page.add(Gtk.Label(label=self.two_d_array[self.labelmenu]))
        self.notebook.append_page(self.new_page, Gtk.Label(label="New Page"))
        self.close_button = Gtk.Button()
        self.close_button.set_image(Gtk.Image(Gtk.STOCK_CLOSE,Gtk.IconSize))
        self.close_button.connect('clicked')
        self.close_button.show()
        self.notebook.show_all()
        
        
window = MyWindow()
window.show_all()

Gtk.main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM