Gtk3.0 Gtkbuttons and junctions sides - python

I'm trying to align many GtkButton with junction sides but the result is not what I want some help please
my code: it is a python code for Gtk3.0 (3.6) I'm on ubuntu 13.04
import gi.repository
from gi.repository import Gtk
def dest(widget):
widget.destroy()
Gtk.main_quit()
win = Gtk.Window(Gtk.WindowType.TOPLEVEL)
hbox = Gtk.Box()
bt0 = Gtk.Button.new_with_label("Zero")
bt1 = Gtk.Button.new_with_label("One")
bt2 = Gtk.Button.new_with_label("Two")
bt0.get_style_context().set_junction_sides(Gtk.JunctionSides.RIGHT)
bt1.get_style_context().set_junction_sides(Gtk.JunctionSides.LEFT|Gtk.JunctionSides.RIGHT)
bt2.get_style_context().set_junction_sides(Gtk.JunctionSides.LEFT)
win.connect("destroy", dest)
hbox.pack_start(bt0, False, False, 0)
hbox.pack_start(bt1, False, False, 0)
hbox.pack_start(bt2, False, False, 0)
win.add(hbox)
win.show_all()
Gtk.main()
thanks

Just add the linked style class to the parent container:
import gi.repository
from gi.repository import Gtk
def dest(widget):
widget.destroy()
Gtk.main_quit()
win = Gtk.Window(Gtk.WindowType.TOPLEVEL)
hbox = Gtk.Box()
hbox.get_style_context().add_class(Gtk.STYLE_CLASS_LINKED)
bt0 = Gtk.Button.new_with_label("Zero")
bt1 = Gtk.Button.new_with_label("One")
bt2 = Gtk.Button.new_with_label("Two")
win.connect("destroy", dest)
hbox.pack_start(bt0, False, False, 0)
hbox.pack_start(bt1, False, False, 0)
hbox.pack_start(bt2, False, False, 0)
win.add(hbox)
win.show_all()
Gtk.main()

Related

How to update widget dynamically in GTK3 (PyGObject)?

In this example, I'm trying to add another button (or any widget) every time a button is pressed.
from gi.repository import Gtk
class ButtonWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Button Demo")
self.hbox = Gtk.HBox()
self.add(self.hbox)
button = Gtk.Button.new_with_label("Click Me")
button.connect("clicked", self.on_clicked)
self.hbox.pack_start(button, False, True, 0)
def on_clicked(self, button):
print("This prints...")
button = Gtk.Button.new_with_label("Another button")
self.hbox.pack_start(button, False, True, 0) # ... but the new button doesn't appear
win = ButtonWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
I have tried queue_draw() and other hacks, but nothing has worked so far.
Calling the show_all() method works to update widgets' children. Here is the code with show_all() used, and the added line marked accordingly:
from gi.repository import Gtk
class ButtonWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Button Demo")
self.hbox = Gtk.HBox()
self.add(self.hbox)
button = Gtk.Button.new_with_label("Click Me")
button.connect("clicked", self.on_clicked)
self.hbox.pack_start(button, False, True, 0)
def on_clicked(self, button):
print("This prints...")
button = Gtk.Button.new_with_label("Another button")
self.hbox.pack_start(button, False, True, 0)
self.hbox.show_all() ### ADDED LINE
win = ButtonWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
So, calling self.hbox.show_all() shows all the children of self.hbox.

How to filter Gtk.Flowbox() children with Gtk.EntrySearch()?

I would like Gtk.FlowBox() to display only children that correspond to the text inserted in Gtk.SearchEntry(), like a search filter.
I think it is possible to do that with Gtk.FlowBox.FilterFunc() and/or Gtk.FlowBox.set_filter_func() but I don't know how and found no example using these functions on the web.
Here is my code:
#!/usr/bin/env python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class FlowBoxWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_border_width(10)
self.set_default_size(300, 200)
header = Gtk.HeaderBar(title="Flow Box")
header.set_subtitle("Flowbox filtering")
header.props.show_close_button = True
self.set_titlebar(header)
box = Gtk.Box()
box.set_orientation(Gtk.Orientation.VERTICAL)
search_entry = Gtk.SearchEntry()
search_entry.connect('search_changed', self.flowbox_filter)
scrolled = Gtk.ScrolledWindow()
scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scrolled.set_min_content_height(200)
self.flowbox = Gtk.FlowBox()
self.flowbox.set_valign(Gtk.Align.START)
self.flowbox.set_max_children_per_line(8)
# Fill flowbox
text = ['ABC','A','BCD','TCUNF','GNCBC','JFABC','LDNAB',
'JJVIC','HZACB','BESEI','VEISEI','GJBVV','abcii','fjbci',
'fsefsi','aabc','fesfoo','fffba','jjfsi'
]
for t in text:
label = Gtk.Label(t)
self.flowbox.add(label)
scrolled.add(self.flowbox)
box.pack_start(search_entry, False, False, 2)
box.pack_start(scrolled, False, False, 2)
self.add(box)
self.show_all()
def flowbox_filter(self, search_entry):
def filter_func(fb_child, text):
if text in [label.get_text() for label in fb_child.get_children()]:
return True
else:
return False
text = search_entry.get_text()
self.flowbox.set_filter_func(filter_func, text)
win = FlowBoxWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Thank you in advance.
I found the solution : instead of searching for the text in FlowBoxChild.get_children(), assign a name to each child while creating them, then check if text match with this name when filtering. It works fine, like that and FlowBox Children get back when clearing search entry field.
Here is the code :
#!/usr/bin/env python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class FlowBoxWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_border_width(10)
self.set_default_size(300, 200)
header = Gtk.HeaderBar(title="Flow Box")
header.set_subtitle("Flowbox filtering")
header.props.show_close_button = True
self.set_titlebar(header)
box = Gtk.Box()
box.set_orientation(Gtk.Orientation.VERTICAL)
search_entry = Gtk.SearchEntry()
search_entry.connect('search_changed', self.flowbox_filter)
scrolled = Gtk.ScrolledWindow()
scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scrolled.set_min_content_height(200)
self.flowbox = Gtk.FlowBox()
self.flowbox.set_valign(Gtk.Align.START)
self.flowbox.set_max_children_per_line(8)
# Fill flowbox
text = ['ABC','A','BCD','TCUNF','GNCBC','JFABC','LDNAB',
'JJVIC','HZACB','BESEI','VEISEI','GJBVV','abcii','fjbci',
'fsefsi','aabc','fesfoo','fffba','jjfsi'
]
for t in text:
label = Gtk.Label(t)
child = Gtk.FlowBoxChild()
child.set_name(t)
child.add(label)
self.flowbox.add(child)
scrolled.add(self.flowbox)
box.pack_start(search_entry, False, False, 2)
box.pack_start(scrolled, False, False, 2)
self.add(box)
self.show_all()
def flowbox_filter(self, search_entry):
def filter_func(fb_child, text):
if text in fb_child.get_name():
return True
else:
return False
text = search_entry.get_text()
self.flowbox.set_filter_func(filter_func, text)
win = FlowBoxWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

gtk+ vlc, MediaPlayerEndReached event does not play next video

So, I'm using GTK+/VLC to create a window with a video playback area and a "next" button.
It starts by playing the first video file and clicking next button will skip to the next media file, playing it. Everything working great so far.
My problem is, if I wait for MediaPlayerEndReached to be fired, button_Next() is executed but the video does not change.
Am I missing anything?
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
gi.require_version('GdkX11', '3.0')
from gi.repository import GdkX11
import vlc
import time
from os import listdir
from os.path import join
startingPath = './files/'
# ----------------------------------
class MediaWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Media Player")
self.set_decorated(False) # removes titlebar
self.move(300,150)
self.connect("destroy", Gtk.main_quit)
self.currFldrIdx = 0
self.currFileIdx = 0
self.Fldrs = []
for fldrs in sorted(listdir(startingPath)):
self.Fldrs.append(join(startingPath,fldrs))
print self.Fldrs
self.num_Fldrs = len(self.Fldrs)
# vlc
self.vlcInstance = vlc.Instance('--no-xlib')
self.vlcPlayer = self.vlcInstance.media_player_new()
def setup_objects_and_events(self):
self.pause_nextImg = Gtk.Image.new_from_icon_name(
"gtk-media-forward",
Gtk.IconSize.MENU
)
# Buttons
self.button_Next = Gtk.Button()
self.button_Next.set_image(self.pause_nextImg)
self.button_Next.connect("clicked", self.on_button_NextImg)
# Area
self.draw_area = Gtk.DrawingArea()
self.draw_area.set_size_request(800,480)
self.draw_area.connect("realize",self._realized)
# Grid ------------------------------------
self.hbox1 = Gtk.Box()
self.hbox1.pack_start(self.draw_area, True, True, 0)
self.hbox2 = Gtk.Box()
self.hbox2.pack_start(self.button_Next, True, True, 0)
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.vbox)
self.vbox.pack_start(self.hbox1, True, True, 0)
self.vbox.pack_start(self.hbox2, False, True, 0)
def on_button_NextImg(self, widget):
print 'next!'
num_FldrFiles = len(listdir(self.Fldrs[self.currFldrIdx]))
self.currFileIdx = self.currFileIdx+1 if self.currFileIdx+1<num_FldrFiles else 0
fileToShow = join(self.Fldrs[self.currFldrIdx], sorted(listdir(self.Fldrs[self.currFldrIdx]))[self.currFileIdx])
print 'now showing' + fileToShow
self.vlcPlayer.set_xwindow(self.win_id)
self.vlcPlayer.set_mrl(fileToShow)
self.vlcPlayer.play()
def _realized(self, widget, data=None):
fileToShow = join(self.Fldrs[self.currFldrIdx], sorted(listdir(self.Fldrs[self.currFldrIdx]))[self.currFileIdx])
self.win_id = widget.get_window().get_xid()
self.vlcPlayer.set_xwindow(self.win_id)
self.vlcPlayer.set_mrl(fileToShow)
self.events = self.vlcPlayer.event_manager()
self.events.event_attach(vlc.EventType.MediaPlayerEndReached, self.EventManager)
self.vlcPlayer.play()
def EventManager(self, event):
if event.type == vlc.EventType.MediaPlayerEndReached:
print "Event reports - finished, playing next"
self.button_Next.clicked()
# ----------------------------------
if __name__ == '__main__':
# Create
win = MediaWindow()
# Setup
win.setup_objects_and_events()
win.show_all()
Gtk.main()
Your problem is described here:
the libvlc API is not reentrant within its callbacks https://forum.videolan.org/viewtopic.php?f=32&t=80305
and here: https://forum.videolan.org/viewtopic.php?t=82502
You should typically have a mainloop in your application (gobject.mainloop(), or Qt mainloop), so you should instead register a method to restart the player from there
This is bit mucked about with for the file names but your code now registers the GObject.idle_add() function that is required.
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
gi.require_version('GdkX11', '3.0')
from gi.repository import GdkX11
import vlc
import time
from os import listdir
from os.path import join
startingPath = './files/'
# ----------------------------------
class MediaWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Media Player")
self.autoPlay = 0
self.set_decorated(False) # removes titlebar
self.move(300,150)
self.connect("destroy", Gtk.main_quit)
self.currFldrIdx = 0
self.currFileIdx = 0
self.Fldrs = ['/home/public/2005.mp3','/home/public/happy.mp3','/home/public/vp.mp3']
#for fldrs in sorted(listdir(startingPath)):
# self.Fldrs.append(join(startingPath,fldrs))
self.num_Fldrs = len(self.Fldrs) - 1
# vlc
self.vlcInstance = vlc.Instance('--no-xlib')
self.vlcPlayer = self.vlcInstance.media_player_new()
def setup_objects_and_events(self):
self.pause_nextImg = Gtk.Image.new_from_icon_name(
"gtk-media-forward",
Gtk.IconSize.MENU
)
# Buttons
self.button_Next = Gtk.Button()
self.button_Next.set_image(self.pause_nextImg)
self.button_Next.connect("clicked", self.on_button_NextImg)
# Area
self.draw_area = Gtk.DrawingArea()
self.draw_area.set_size_request(800,480)
self.draw_area.connect("realize",self._realized)
# Grid ------------------------------------
self.hbox1 = Gtk.Box()
self.hbox1.pack_start(self.draw_area, True, True, 0)
self.hbox2 = Gtk.Box()
self.hbox2.pack_start(self.button_Next, True, True, 0)
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.vbox)
self.vbox.pack_start(self.hbox1, True, True, 0)
self.vbox.pack_start(self.hbox2, False, True, 0)
def on_button_NextImg(self, widget=None):
self.currFileIdx += 1
if self.currFileIdx > self.num_Fldrs:
self.currFileIdx = 0
fileToShow = self.Fldrs[self.currFileIdx]
media = self.vlcInstance.media_new_path(fileToShow)
self.vlcPlayer.set_media(media)
if self.vlcPlayer.play() == -1:
print ("error playing",fileToShow)
else:
print("now playing",fileToShow)
def _realized(self, widget, data=None):
fileToShow = self.Fldrs[self.currFldrIdx]
self.win_id = widget.get_window().get_xid()
self.vlcPlayer.set_xwindow(self.win_id)
self.vlcPlayer.set_mrl(fileToShow)
self.events = self.vlcPlayer.event_manager()
self.events.event_attach(vlc.EventType.MediaPlayerEndReached, self.EventManager)
self.vlcPlayer.play()
def EventManager(self, event):
if event.type == vlc.EventType.MediaPlayerEndReached:
GObject.idle_add(self.on_button_NextImg)
# ----------------------------------
if __name__ == '__main__':
# Create
win = MediaWindow()
# Setup
win.setup_objects_and_events()
win.show_all()
Gtk.main()

How to create PyGObject application with a menubar using Gtk.Builder?

There is no full documentation about how to use Gtk.Builder in PyGObject to create a menubar.
I don't use that Gtk.UIManager because it is deprecated.
The example code below is based on my experience with Gtk.UIManager.
In the example should appear a menubar with Foo as a top menu group having an clickable item Bar.
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
class Window(Gtk.ApplicationWindow):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 100)
#
self.interface_info = """
<interface>
<menu id='TheMenu'>
<section>
<attribute name='foo'>Foo</attribute>
<item>
<attribute name='bar'>Bar</attribute>
</item>
</section>
</menu>
</interface>
"""
builder = Gtk.Builder.new_from_string(self.interface_info, -1)
action_bar = Gio.SimpleAction.new('bar', None)
action_bar.connect('activate', self.on_menu)
self.add_action(action_bar)
menubar = builder.get_object('TheMenu')
# layout
self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.layout.pack_start(menubar, True, True, 0)
self.add(self.layout)
self.connect('destroy', Gtk.main_quit)
self.show_all()
def on_menu(self, widget):
print(widget)
if __name__ == '__main__':
win = Window()
Gtk.main()
The current error is
Traceback (most recent call last):
File "./_menubar.py", line 46, in <module>
win = Window()
File "./_menubar.py", line 36, in __init__
self.layout.pack_start(menubar, True, True, 0)
TypeError: argument child: Expected Gtk.Widget, but got gi.repository.Gio.Menu
I am unsure about
How to create the XML string.
How to get the menubar-widget.
How to create Actions/Click-handlers for menu items.
Of course the question could be extended to toolbars but I wouldn't made it to complexe.
btw: I don't want to use Gtk.Application.set_menubar(). Because there is no Gtk.Application.set_toolbar() and currently I see no advantage on having a Gtk-based application object.
EDIT: I also tried this variant (without any success):
gio_menu = builder.get_object('TheMenu')
menubar = Gtk.Menubar.new_from_model(gio_menu)
My answer is based on a foreign answer on the gtk-dev-app mailinglist.
I prefere Variant 3.
Variant 1: with XML-String
Please be aware of the different naming of the action between the XML-string (win.bar) and the Gio.SimpleAction(bar).
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
class Window(Gtk.ApplicationWindow):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 100)
#
self.interface_info = """
<interface>
<menu id='TheMenu'>
<submenu>
<attribute name='label'>Foo</attribute>
<item>
<attribute name='label'>Bar</attribute>
<attribute name='action'>win.bar</attribute>
</item>
</submenu>
</menu>
</interface>
"""
builder = Gtk.Builder.new_from_string(self.interface_info, -1)
action_bar = Gio.SimpleAction.new('bar', None)
action_bar.connect('activate', self.on_menu)
self.add_action(action_bar)
menumodel = builder.get_object('TheMenu')
menubar = Gtk.MenuBar.new_from_model(menumodel)
# layout
self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.layout.pack_start(menubar, False, False, 0)
self.add(self.layout)
self.connect('destroy', Gtk.main_quit)
self.show_all()
def on_menu(self, action, value):
print('Action: {}\nValue: {}'.format(action, value))
if __name__ == '__main__':
win = Window()
Gtk.main()
Variant 2: without XML but with Actions
I prefere this variant because it doesn't use (human unreadable XML) and Gtk.Builder.
Here you create the structure of your menu as a data structure based on Gio.Menu and connect a Action (which itself is connected to an event handler) to it's items. Out of that informations the widget for the menubar is kind of generated.
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
class Window(Gtk.ApplicationWindow):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 100)
action_bar = Gio.SimpleAction.new('bar', None)
action_bar.connect('activate', self.on_menu)
self.add_action(action_bar)
# root of the menu
menu_model = Gio.Menu.new()
# menu item "Bar"
menu_item = Gio.MenuItem.new('Bar', 'win.bar')
# sub-menu "Foo" with item "Bar"
menu_foo = Gio.Menu.new()
menu_foo.append_item(menu_item)
menu_model.append_submenu('Foo', menu_foo)
# create menubar widget from the model
menubar = Gtk.MenuBar.new_from_model(menu_model)
# layout
self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.layout.pack_start(menubar, False, False, 0)
self.add(self.layout)
self.connect('destroy', Gtk.main_quit)
self.show_all()
def on_menu(self, action, value):
print('Action: {}\nValue: {}'.format(action, value))
if __name__ == '__main__':
win = Window()
Gtk.main()
Variant 3: Old-school, easy without XML, Actions or Gio layer
This variant works kind of "old school" because you simply build your menu widgets together and connect signalls directly to them. This works without using a underlying and abstract data structure (e. g. Gio.MenuModel or an XML-string) and without a Application class.
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 100)
# create menubar
menubar = self._create_menubar()
# create a toolbar
toolbar = self._create_toolbar()
# layout
self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.layout.pack_start(menubar, False, False, 0)
self.layout.pack_start(toolbar, False, False, 0)
self.add(self.layout)
self.connect('destroy', Gtk.main_quit)
self.show_all()
def _create_menubar(self):
# menu item 'Bar'
item_bar = Gtk.MenuItem.new_with_label('Bar')
item_bar.connect('activate', self.on_menu)
# sub menu for 'Bar'
menu_foo = Gtk.Menu.new()
menu_foo.append(item_bar)
# main menu 'Foo' with attached sub menu
item_foo = Gtk.MenuItem.new_with_label('Foo')
item_foo.set_submenu(menu_foo)
# the menubar itself
menubar = Gtk.MenuBar.new()
menubar.append(item_foo)
return menubar
def _create_toolbar(self):
toolbar = Gtk.Toolbar.new()
# button with label
bar_item = Gtk.ToolButton.new(None, 'Bar')
bar_item.connect('clicked', self.on_menu)
toolbar.insert(bar_item, -1)
# button with icon
bar_item = Gtk.ToolButton.new_from_stock(Gtk.STOCK_OK)
bar_item.connect('clicked', self.on_menu)
toolbar.insert(bar_item, -1)
return toolbar
def on_menu(self, caller):
print(caller)
if __name__ == '__main__':
win = Window()
Gtk.main()

Python Gtk3 Box doesn't hide

I'm writing an application. It has a sidebar, which I want to be shut on startup. I have tried using self.sidebarbox.hide(). This only seems to work when I put it in a function that is linked to a button. When I press the button it shows/hides. How can I fix this?
Here is my code (It's written in python3 but will run in python2.):
from gi.repository import Gtk, Gio
from gi.repository import WebKit
HEIGHT = 500
WIDTH = 800
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Resolution")
self.set_border_width(0)
self.set_default_size(WIDTH, HEIGHT)
hb = Gtk.HeaderBar()
hb.props.show_close_button = True
hb.props.title = "Resolution"
hb.props.subtitle = "Digital Maths Revision Guide"
self.set_titlebar(hb)
button = Gtk.Button()
icon = Gio.ThemedIcon(name="emblem-system-symbolic")
image = Gtk.Image.new_from_gicon(icon, 1)
button.add(image)
button.connect("clicked", self.sidebarShowHide)
button.set_focus_on_click(False)
hb.pack_start(button)
self.sidebarbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
toplevelbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.add(toplevelbox)
toplevelbox.pack_start(self.sidebarbox, False, False, 0)
self.searchentry = Gtk.SearchEntry()
self.searchentry.connect("search-changed", self.search_changed)
self.sidebarbox.pack_start(self.searchentry, False, False, 0)
label = Gtk.Label("Contents Selector")
self.sidebarbox.pack_start(label, True, True, 0)
scroller = Gtk.ScrolledWindow()
content = WebKit.WebView()
scroller.add(content)
toplevelbox.pack_start(scroller, True, True, 0)
content.open("/home/oliver/resolution/placeholder.html")
#This should make the sidebar hide.
self.sidebarbox.hide()
#This works. The sidebar does show/hide.
def sidebarShowHide(self, button):
if self.sidebarbox.get_visible():
self.sidebarbox.hide()
else:
self.sidebarbox.show()
def search_changed(self, searchentry):
pass
win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
You're calling show_all() which changes the state of all contained widgets to visible, including the sidebar.
If you still like to use it (it's convenient after all) one way is you're own method, which will hide the sidebar after showing all, e.g:
from gi.repository import Gtk, Gio
from gi.repository import WebKit
HEIGHT = 500
WIDTH = 800
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Resolution")
self.set_border_width(0)
self.set_default_size(WIDTH, HEIGHT)
hb = Gtk.HeaderBar()
hb.props.show_close_button = True
hb.props.title = "Resolution"
hb.props.subtitle = "Digital Maths Revision Guide"
self.set_titlebar(hb)
button = Gtk.Button()
icon = Gio.ThemedIcon(name="emblem-system-symbolic")
image = Gtk.Image.new_from_gicon(icon, 1)
button.add(image)
button.connect("clicked", self.sidebarShowHide)
button.set_focus_on_click(False)
hb.pack_start(button)
self.sidebarbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
toplevelbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.add(toplevelbox)
toplevelbox.pack_start(self.sidebarbox, False, False, 0)
self.searchentry = Gtk.SearchEntry()
self.searchentry.connect("search-changed", self.search_changed)
self.sidebarbox.pack_start(self.searchentry, False, False, 0)
label = Gtk.Label("Contents Selector")
self.sidebarbox.pack_start(label, True, True, 0)
scroller = Gtk.ScrolledWindow()
content = WebKit.WebView()
scroller.add(content)
toplevelbox.pack_start(scroller, True, True, 0)
content.open("/home/oliver/resolution/placeholder.html")
def inital_show(self):
win.show_all()
self.sidebarbox.hide();
#This works. The sidebar does show/hide.
def sidebarShowHide(self, button):
if self.sidebarbox.get_visible():
self.sidebarbox.hide()
else:
self.sidebarbox.show()
def search_changed(self, searchentry):
pass
if __name__ == '__main__':
win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.inital_show()
Gtk.main()
Note the initial_show() method, and calling it from the main section.

Categories