0

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. I am trying to make a browser-like interface using the Gtk notebook. 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. 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)>. 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. 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!

1
  • At the end of create_page function, add self.show_all(). Commented Aug 16, 2020 at 3:12

2 Answers 2

2

As jackw11111 said, the solution was to add self.show_all() at the end of create_page function. Thanks so much!

I made an answer so anyone with this same problem could easily find the answer.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

hmm doesn't seem to work for me.. Thanks for trying though! I get this error when I try and make a new page: Hello Traceback (most recent call last): File "sticky-notes.py", line 68, in on_click_popup self.close_button.set_image(Gtk.Image(Gtk.STOCK_CLOSE,Gtk.IconSize)) TypeError: GObject.__init__() takes exactly 0 arguments (2 given)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.