Gtk3 and Python - Box doesn't resize to fill window - python

I'm writing an app using Gtk3 and Python. I have a revealer as a sidebar to select the content and a webkit webview to display the main content. When the revealer is hidden the webview doesn't fill the entire window space and I don't know why. Any help would be appreciated.
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"
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)
sidebarbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
toplevelbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.add(toplevelbox)
self.sidebar = Gtk.Revealer()
self.sidebar.set_transition_duration(0)
self.sidebar.set_reveal_child(False)
toplevelbox.pack_start(self.sidebar, False, False, 0)
self.sidebar.add(sidebarbox)
self.searchentry = Gtk.SearchEntry()
self.searchentry.connect("search-changed", self.search_changed)
sidebarbox.pack_start(self.searchentry, False, False, 0)
label = Gtk.Label("Contents Selector")
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 sidebarShowHide(self, button):
if self.sidebar.get_reveal_child():
self.sidebar.set_reveal_child(False)
else:
self.sidebar.set_reveal_child(True)
def search_changed(self, searchentry):
pass
win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Well, i have done some GtkRevealer few months ago and it works. It drive me nuts to see this piece of code was not.
I opened my project again and look inside where that part is, and it turn out the toplevel container where the Gtk.Revealer resides, has to has Gtk.Orientation.VERTICAL.. if you change your "toplevelbox" orientation to that, it will work, but it wont be sidebar. It will coming from top or bottom. It goes the same if you change GtkBox with GtkGrid. If I were to guess it depends on the children default orientation.
Workaround on that, is to use widget hide/show mechanism (believe me, i ransack your code and it works).
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"
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)
sidebarbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
toplevelbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.add(toplevelbox)
self.sidebar = Gtk.Box()
toplevelbox.pack_start(self.sidebar, False, False, 0)
self.sidebar.add(sidebarbox)
self.searchentry = Gtk.SearchEntry()
self.searchentry.connect("search-changed", self.search_changed)
sidebarbox.pack_start(self.searchentry, False, False, 0)
label = Gtk.Label("Contents Selector")
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 sidebarShowHide(self, button):
if self.sidebar.get_visible():
self.sidebar.hide ()
else:
self.sidebar.show ()
def search_changed(self, searchentry):
pass
win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

This smells like a bug.
As a workaround, you could use
def sidebarShowHide(self, button):
self.sidebarbox.set_visible(not self.sidebarbox.get_visible())
but this does not yield any transition animation, but does remove its allocated space for the time being invisible.
Actually the C demo provided within the git repo resizes as expected, so this might in fact have to do something with the child widgets prefered orientation.

Related

Python3 Gtk new tab

I'm just playing with Gtk currently, but was wondering if someone has an example code of a window that has a notebook in it, and with a click of a button or event, it opens a new tab with a Gtk (for example) entry in it and is accessible from further code.
I can't find any working code like that on the web or on this website.
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Simple Notebook Example")
self.set_border_width(3)
self.box = Gtk.VBox(spacing=6)
self.add(self.box)
self._add_tab_button = Gtk.Button(label="Add Tab")
self._add_tab_button.connect("clicked", self.add_tabs)
self.box.pack_start(self._add_tab_button, False, False, 5)
self.counter = 1
self.notebook = Gtk.Notebook()
self.box.pack_start(self.notebook, True, True, 5)
# add two tabs
self.add_tabs(None)
self.add_tabs(None)
def add_tabs(self, button):
page = Gtk.Box()
page.set_border_width(10)
page.add(Gtk.Label(label="Page %s content" % self.counter))
self.notebook.append_page(
page, Gtk.Label(label="Page %s" % self.counter))
page.show_all()
self.counter += 1
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

PyGTK resize image

I would like to resize (not automatically) an image, I used .set_size_request but it made no changes.
from gi.repository import Gtk
window = Gtk.Window()
window.set_size_request(320, 240)
window.connect('delete-event', Gtk.main_quit)
box = Gtk.Box(spacing=6)
image = Gtk.Image()
image.set_from_file("C:\\Users\\alimacher\\FF0000.png")
image.set_size_request(400, 400)
box.pack_start(image, False, False, 1)
window.add(box)
window.show_all()
Gtk.main()
And please there is a way to do it without PixBuf ?
Thank you.

GtkOverlay hides GtkTextView

I'm trying to develop an application using Gtk, and I have run into a problem using GtkOverlay. If I have a GtkOverlay with a GtkTextView that was added using the standard container add method, the text is hidden. However, all other widgets, say for example, buttons, appear just fine. Even more odd is the fact that this behavior is only present if at least one widget was adding using add_overlay.
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
USE_OVERLAY = False
win = Gtk.Window()
text_view = Gtk.TextView()
overlay = Gtk.Overlay()
top_button = Gtk.Button()
bottom_button = Gtk.Button()
top_container = Gtk.VBox()
bottom_container = Gtk.VBox()
overlay_str = "( USE_OVERLAY = " + str(USE_OVERLAY) + ")"
win.set_title(overlay_str)
top_button.set_label("I'm a button on top!")
bottom_button.set_label("I'm a button on bottom!")
text_view.get_buffer().set_text("This should be visible")
win.add(overlay)
overlay.add(bottom_container)
bottom_container.pack_start(bottom_button, False, False, 0)
bottom_container.pack_end(text_view, True, True, 0)
if USE_OVERLAY:
overlay.add_overlay(top_container)
top_container.pack_end(top_button, False, False, 0)
win.connect("delete-event", Gtk.main_quit)
overlay.show_all()
win.show_all()
Gtk.main()
I have reason to believe that this is not a python problem, as the actual application is written using haskell-gi, however I figured more people would be familiar with python.
I don't know on what system your a running this example but it is working fine for me. The only caveat is that the top button appears over the bottom button and the TextView widget so I have to manually resize the Window to see the text. You can see a screen cast of my situation in this video: https://youtu.be/xoAH4OuEM0E
Now depending on what you really want there may be few different answers. What I would suggest is putting the TextView inside a ScrolledWindow. This way the TextView will be at least visible before you would need the resize the window. It would also have the consequence to provide scrollbars if the text overflow the visible area.
It could look like this:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
USE_OVERLAY = True
win = Gtk.Window()
text_view = Gtk.TextView()
overlay = Gtk.Overlay()
top_button = Gtk.Button()
bottom_button = Gtk.Button()
top_container = Gtk.VBox()
bottom_container = Gtk.VBox()
overlay_str = "( USE_OVERLAY = " + str(USE_OVERLAY) + ")"
win.set_title(overlay_str)
top_button.set_label("I'm a button on top!")
bottom_button.set_label("I'm a button on bottom!")
text_view.get_buffer().set_text("This should be visible")
# This is where the text_view is inserted in a ScrolledWindow
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.add(text_view)
win.add(overlay)
overlay.add(bottom_container)
bottom_container.pack_start(bottom_button, False, False, 0)
# The scrolled_window is inserted in the bottom_container
bottom_container.pack_end(scrolled_window, True, True, 0)
if USE_OVERLAY:
overlay.add_overlay(top_container)
top_container.pack_end(top_button, False, False, 0)
win.connect("delete-event", Gtk.main_quit)
overlay.show_all()
win.show_all()
Gtk.main()
You can also see the result on the aforementioned screencast. The only drawback is that the top button won't be able to overlay as much the bottom layer as in your script. But maybe it won't bother you.

pygobject add item to container within signal callback

I'm working on a simple GUI application using PyGObject and GTK+ 3. In this case, I'm wanting to have a button which brings up a dialog box that when you click OK will add an item to a list. I have that part working but the final part that doesn't work is adding the item to the list. It appears that an item does get added but it's empty. It's selectable, though, just very small. I've tried adding other kinds of widgets like Gtk.Button to see if it was something weird with Gtk.Label. When I add the Gtk.Label in the constructor it works just fine.
Also I know this isn't quite the way to do things and there are some oddities with how I'm doing stuff in my code but I'm still just learning how to use PyGObject/GTK+ 3. I imagine this problem is just something stupid I'm overlooking.
MainWindow.py
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import PromptDialog
class MainWindow(Gtk.Window):
def addURLResponse(self, dialog, response, listBox):
if(response == Gtk.ResponseType.OK):
print(dialog.get_text())
label = Gtk.Label(dialog.get_text())
print(label.get_text())
listBox.add(label)
if(response != Gtk.ResponseType.DELETE_EVENT):
dialog.destroy()
def addURL(self, button):
URLDialog = PromptDialog.PromptDialog("Add URL", self)
URLDialog.connect('response', self.addURLResponse, button.get_parent().get_parent().get_parent().get_children()[1])
URLDialog.show_all()
def __init__(self):
Gtk.Window.__init__(self, title="MPV-VJ")
self.playlistsBar = Gtk.FlowBox()
self.newBtn = Gtk.Button.new_with_label('+')
self.playlistsBar.add(self.newBtn)
self.playlistsList = Gtk.ListBox()
self.playlistsView = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.playlistsView.pack_start(self.playlistsBar, False, False, 0)
self.playlistsView.pack_start(self.playlistsList, True, True, 0)
self.playlist1View = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.playlist1Bar = Gtk.FlowBox()
self.addUrl1Btn = Gtk.Button.new_with_label('+URL')
self.addUrl1Btn.connect('clicked', self.addURL)
self.playlist1Bar.add(self.addUrl1Btn)
self.addFile1Btn = Gtk.Button.new_with_label('+file')
self.playlist1Bar.add(self.addFile1Btn)
self.addDir1Btn = Gtk.Button.new_with_label('+dir')
self.playlist1Bar.add(self.addDir1Btn)
self.playlist1List = Gtk.ListBox()
self.playlist1View.pack_start(self.playlist1Bar, False, False, 0)
self.playlist1View.pack_start(self.playlist1List, True, True, 0)
self.playlist2View = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.playlist2Bar = Gtk.FlowBox()
self.addUrl2Btn = Gtk.Button.new_with_label('+URL')
self.playlist2Bar.add(self.addUrl2Btn)
self.addFile2Btn = Gtk.Button.new_with_label('+file')
self.playlist2Bar.add(self.addFile2Btn)
self.addDir2Btn = Gtk.Button.new_with_label('+dir')
self.playlist2Bar.add(self.addDir2Btn)
self.playlist2List = Gtk.ListBox()
self.playlist2View.pack_start(self.playlist2Bar, False, False, 0)
self.playlist2View.pack_start(self.playlist2List, True, True, 0)
self.plViewsBox = Gtk.HPaned()
self.plViewsBox.pack1(self.playlist1View, True, False)
self.plViewsBox.pack2(self.playlist2View, True, False)
self.viewBox = Gtk.HPaned()
self.viewBox.pack1(self.playlistsView, True, False)
self.viewBox.pack2(self.plViewsBox, True, False)
self.viewBox.set_position(200)
self.logView = Gtk.ListBox()
self.contentBox = Gtk.VPaned()
self.contentBox.pack1(self.viewBox, True, False)
self.contentBox.pack2(self.logView, True, False)
self.contentBox.set_position(400)
self.toolBar = Gtk.FlowBox()
self.newBtn = Gtk.Button.new_with_label('new')
self.toolBar.add(self.newBtn)
self.loadBtn = Gtk.Button.new_with_label('load')
self.toolBar.add(self.loadBtn)
self.saveBtn = Gtk.Button.new_with_label('save')
self.toolBar.add(self.saveBtn)
self.mainBox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
self.mainBox.pack_start(self.toolBar, False, False, 0)
self.mainBox.pack_start(self.contentBox, True, True, 0)
self.add(self.mainBox)
self.resize(1000, 500)
PromptDialog.py
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class PromptDialog(Gtk.Dialog):
def get_text(self):
return(self.entry.get_buffer().get_text())
def __init__(self, message, mainWindow):
Gtk.Dialog.__init__(self, title="Prompt")
self.set_modal(True)
self.set_transient_for(mainWindow)
self.label = Gtk.Label(message)
self.entry = Gtk.Entry()
self.get_content_area().pack_start(self.label, True, True, 0)
self.get_content_area().pack_start(self.entry, True, True, 0)
self.add_button("OK", Gtk.ResponseType.OK)
self.add_button("Cancel", Gtk.ResponseType.CANCEL)
The widget added to the window needs to have its .show() method called before it'll appear.

How to get background of textview in pygobject “gtk3”?

I'd like to get the current background color of my textview to change it and restore it later.
here what I tried:
context = textview.get_style_context()
state = Gtk.StateFlags.NORMAL
color = context.get_background_color(state)
I tried all possible states, but none returns the correct background color (white in my case)
Any idea how to get it?
I'm not exactly sure what you specific problem is without seeing more code, but here is a quick example that overrides the background and then restores it on a button click:
from gi.repository import Gtk, Gdk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Textview example", application=app)
self.set_default_size(250, 100)
self.set_border_width(10)
self.view = Gtk.TextView()
self.style_context = self.view.get_style_context()
self.default_bg_color = self.style_context.get_background_color(Gtk.StateFlags.NORMAL)
self.view.override_background_color(Gtk.StateFlags.NORMAL,
Gdk.RGBA(0, 0, 0, 1))
self.btn = Gtk.Button(label="Click Here")
self.btn.connect("clicked", self.on_btn_clicked)
box = Gtk.VBox()
box.pack_start(self.view, True, True, 0)
box.pack_start(self.btn, False, False, 0)
self.add(box)
def on_btn_clicked(self, widget):
current_bg = self.style_context.get_background_color(Gtk.StateFlags.NORMAL)
if current_bg == self.default_bg_color:
self.view.override_background_color(Gtk.StateFlags.NORMAL,
Gdk.RGBA(0, 0, 0, 1))
else:
self.view.override_background_color(Gtk.StateFlags.NORMAL,
self.default_bg_color)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

Categories

Resources