Python Gtk3 Box doesn't hide - python

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.

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.

Python GTK3: button with image and label and get the Label value after click event

I'm trying to get the label value that is inside a Grid and that grid is inside a button after a click event.
This is my part of code:
for one_text in text_list:
label_for_button = Gtk.Label(one_text)
label_for_button.set_line_wrap(True)
image_for_button = Gtk.Image.new_from_file("img.png")
grid_in_button = Gtk.Grid()
grid_in_button.add(image_button)
grid_in_button.attach_next_to(label_for_button, image_for_button, Gtk.PositionType.BOTTOM, 1, 2)
grid_in_button.show_all()
button.add(grid_in_button)
button.connect("clicked", self.on_button_clicked)
def on_button_clicked(self, widget):
# here i wanna get the value of the label_for_button
Help.. Any idea? Thanks
hope this code helps:
import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk,GdkPixbuf
def btn_clicked(widget):
print(widget.get_label())
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename="img.png", width=24, height=24, preserve_aspect_ratio=True)
img = Gtk.Image.new_from_pixbuf(pixbuf)
btn = Gtk.Button(label='some text',image=img,)
btn.connect('clicked',btn_clicked)
win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
win.add(btn)
win.show_all()
Gtk.main()

How to properly show and hide GTK window in python

So I have the following code (python 2.7) that checks for a combination of keypresses and shows/hides a gtk window with a few gtk entry boxes. The show/hide works until the window locks up and stops responding, and the inner part of the window turns black. I have to kill the process and start it up again once that window turns black. I've tried all different combinations of show_all() and returning True after hiding the window to no avail. I'm not sure what I'm doing wrong here.
In the end I'd like to be able to press this key combination and show/hide this window without the contents going away.
#!/usr/bin/python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import pyxhook
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Configurator")
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.ipEntry = Gtk.Entry()
self.ipEntry.set_text("IP Address")
self.maskEntry = Gtk.Entry()
self.maskEntry.set_text("NetMask")
self.gatewayEntry = Gtk.Entry()
self.gatewayEntry.set_text("gatewayEntry")
self.button1 = Gtk.Button(label="Save")
self.button1.connect("clicked", self.on_button1_clicked)
self.box.pack_start(self.ipEntry, True, True, 0)
self.box.pack_start(self.maskEntry, True, True, 0)
self.box.pack_start(self.gatewayEntry, True, True, 0)
self.box.pack_start(self.button1, True, True, 0)
#catch window destroy event and stop it from happening
self.connect('delete-event', self.on_destroy)
def on_button1_clicked(self, widget):
print("Hello")
def on_destroy(self, widget=None, *data):
print("tried to destroy")
self.hide()
return True
#list of ascii keypresses to test if a certain combination of keys is pressed
keypresses = []
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.set_keep_above(True)
def OnKeyboardEvent(event):
#ascii 227 is l_control, ascii 225 is l_shift, ascii 120 is x
#bring the following external variables into the scope of this function
global keypresses
global win
#check if gtk window is hidden or visible
isVisible = win.get_property("visible")
#store our keypress if it is worthy of being stored (we started with a control character)
if event.Ascii == 227 or ( len(keypresses) >= 1 and keypresses[0] == 227 ):
print("here" + str(len(keypresses)))
keypresses.append(event.Ascii)
#check if we have three items in keypresses to compare, then see if it's the right combination
if len(keypresses) == 3 and keypresses[0] == 227 and keypresses[1] == 225 and keypresses[2] == 120:
#show/hide our window
if isVisible:
win.hide()
del keypresses[:]
keypresses = []
else:
win.show_all()
#clear out our list to compare again
del keypresses[:]
keypresses = []
elif len(keypresses) >= 3:
del keypresses[:]
keypresses = []
#create instance of hook manager
HookManager = pyxhook.HookManager()
#define our callback to fire when a key is pressed
HookManager.KeyDown = OnKeyboardEvent
#hook the keyboard
HookManager.HookKeyboard()
#start our listener
HookManager.start()
Gtk.main()
#close the hook listener when gtk main loop exits
HookManager.cancel()
Here is a small hack that tests the window show/hide when pressing F5. When you press it a second window will show or hide and i've tested for sometime without problems. Maybe it's the HookManager that somehow is messing with the Gtk/Glib mainloop. Nonetheless my method only works if there is a window to grab the key presses, so you will need, indeed, some form of grabbing key presses if your goal is to have no GUI:
#!/usr/bin/python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
#import pyxhook
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Configurator")
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.ipEntry = Gtk.Entry()
self.ipEntry.set_text("IP Address")
self.maskEntry = Gtk.Entry()
self.maskEntry.set_text("NetMask")
self.gatewayEntry = Gtk.Entry()
self.gatewayEntry.set_text("gatewayEntry")
self.button1 = Gtk.Button(label="Save")
self.button1.connect("clicked", self.on_button1_clicked)
self.box.pack_start(self.ipEntry, True, True, 0)
self.box.pack_start(self.maskEntry, True, True, 0)
self.box.pack_start(self.gatewayEntry, True, True, 0)
self.box.pack_start(self.button1, True, True, 0)
#catch window destroy event and stop it from happening
self.connect('delete-event', self.on_destroy)
self.connect('key-press-event', self.on_keypress)
def on_keypress(self, widget, event):
global w
if event.keyval == Gdk.KEY_F5:
isVisible = w.get_property("visible")
if (isVisible):
w.hide()
else:
w.show()
print("Keypress")
def on_button1_clicked(self, widget):
print("Hello")
def on_destroy(self, widget=None, *data):
print("tried to destroy")
self.hide()
return False
w = MyWindow()
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.set_keep_above(True)
Gtk.main()

Gtk3.0 Gtkbuttons and junctions sides

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

could not add table inside vbox pygtk

I have this code to add table inside VBox which is not working
topwid=gtk.VBox(gtk.FALSE, 0)
table = gtk.Table(3, 3, True)
topwid.pack_start(table)
how to do this?
Source code(GUI part):
def build_gui(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("delete_event", self.gotoTray)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(24)
self.window.set_size_request(400, 200)
self.window.set_title("xyz client")
self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
self.window.set_resizable(False)
topwid=gtk.VBox(gtk.FALSE, 0)
table = gtk.Table(3, 3, True)
topwid.pack_start(table)
label = gtk.Label("Username")
table.attach(label,0,1,0,1)
label.show()
self.username=gtk.Entry(max=10)
table.attach(self.username,1,3,0,1)
self.username.show()
label1 = gtk.Label("Password")
table.attach(label1,0,1,1,2)
label1.show()
self.password=gtk.Entry(max=10)
table.attach(self.password,1,3,1,2)
self.password.set_visibility(False)
self.password.show()
self.button=gtk.Button("Login")
self.button.connect("clicked",self.click_button)
table.attach(self.button,1,3,2,3)
self.button.show()
#
table.show()
mb = gtk.MenuBar()
filemenu = gtk.Menu()
filem = gtk.MenuItem("File")
filem.set_submenu(filemenu)
exit = gtk.MenuItem("Exit")
exit.connect("activate", gtk.main_quit)
filemenu.append(exit)
mb.append(filem)
vbox = gtk.VBox(False, 2)
vbox.pack_start(mb, False, False, 0)
topwid.pack_start(vbox)
topwid.show()
self.statusicon = gtk.status_icon_new_from_stock(gtk.STOCK_NETWORK)
self.statusicon.connect('activate', self.status_clicked )
self.statusicon.set_name("Cyberoam")
self.window.show_all()
gtk.main()
Could do with seeing some more of the source code to really be sure but are you calling show_all on the window object?
Below is an example of it working.
import pygtk
pygtk.require('2.0')
import gtk
class Base:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
vbox=gtk.VBox(False, 0)
table = gtk.Table(3, 3, True)
table.attach(gtk.Button("1"), 0, 1, 0, 1)
vbox.pack_start(table)
self.window.add(vbox)
self.window.show_all()
def main(self):
gtk.main()
if __name__ == "__main__":
base = Base()
base.main()

Categories