I'm trying to set the focus to an Entry input field. If I put it inside a Box, I can set the focus via the grab_focus method. But if the Entry is inside a Notebook, it is not focused.
Example code:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Simple Notebook Example")
self.set_border_width(3)
self.notebook = Gtk.Notebook()
self.add(self.notebook)
self.page1 = Gtk.Box()
self.page1.set_border_width(10)
self.page1.add(Gtk.Label(label="Default Page!"))
self.notebook.append_page(self.page1, Gtk.Label(label="Plain Title"))
self.note = Gtk.Entry()
self.note.set_activates_default(True)
self.note.set_text("")
self.page1.add(self.note)
self.page2 = Gtk.Box()
self.page2.set_border_width(10)
self.page2.add(Gtk.Label(label="A page with an image for a Title."))
self.notebook.append_page(
self.page2, Gtk.Image.new_from_icon_name("help-about", Gtk.IconSize.MENU)
)
self.note.grab_focus() # does not work
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
How can I focus self.note inside the notebook?
Thanks for any help!
As it turns out, you can only grab focus to visible widgets.
Calling grab_focus after show_all makes the example work:
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
win.note.grab_focus()
Gtk.main()
Related
Please consider this python code for a simple GTK window:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def on_destroy(widget):
print("Destroying {}".format(widget))
def on_click(button, scroll):
scroll.destroy()
scroll = Gtk.ScrolledWindow()
scroll.connect("destroy", on_destroy)
button = Gtk.Button.new_with_label("Self-destruct")
scroll.add(button)
button.connect("destroy", on_destroy)
button.connect("clicked", on_click, scroll)
window = Gtk.Window()
window.add(scroll)
window.connect("destroy", Gtk.main_quit)
window.set_size_request(200, 75)
window.show_all()
Gtk.main()
When pressing the button, I would expect to get two output lines reporting that the ScrolledWindow and the button got destroyed. In reality I only get one line reporting about the ScrolledWindow, the destroy signal never reaches the button. If I replace the ScrolledWindow with a normal Box, the destroy signal reaches the button. Why is this?
I really can't tell you much more than this code which does delete the button. Hopefully, you can adapt it to your needs. It does seem like the button unattaches itself from the Viewport and attaches itself to nothing, because the button's destroy handler runs on app shutdown, and causes weird errors. It would be interesting to post this on the Gtk mailing list for any other thoughts.
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def on_destroy(widget, viewport):
print("Destroying {}".format(widget))
viewport.get_child().destroy()
def on_button_destroy(widget):
print("Destroying button {}".format(widget))
def on_click(button):
scroll = button.get_parent().get_parent()
scroll.destroy()
scroll = Gtk.ScrolledWindow()
button = Gtk.Button.new_with_label("Self-destruct")
scroll.add(button)
viewport = button.get_parent()
button.connect("destroy", on_button_destroy)
scroll.connect("destroy", on_destroy, viewport)
button.connect("clicked", on_click)
window = Gtk.Window()
window.add(scroll)
window.connect("destroy", Gtk.main_quit)
window.set_size_request(200, 75)
window.show_all()
Gtk.main()
Can anyone tell me how I can have a preference pop-up stay centered on parent window even while moving the parent window? Thank you :)
Here is a very simple example, the preference window is centered and while moving it the parent will also move (but will depend on window manager):
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class AppWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Python Example")
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,spacing=6)
self.add(box)
button1 = Gtk.Button("Information")
button1.connect("clicked", self.on_button_clicked)
box.add(button1)
treeview = Gtk.TreeView()
treeview.set_size_request(640,480)
box.add(treeview)
def on_button_clicked(self, widget):
preferences_window = Gtk.Window (Gtk.WindowType.TOPLEVEL)
preferences_window.set_title ("Preferences Window")
preferences_window.set_modal (True)
preferences_window.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
preferences_window.set_transient_for(self)
preferences_window.show_all()
preferences_window.connect("delete-event", preferences_window.destroy);
win = AppWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Resulting in:
How do I get Gtk.scrolledwindow to scroll to a selection in Gtk.Treeview.
I am writing a touch screen kiosk app that has up and down button to move a selection in a treeview.
It doesn't it doesn't scroll down the scrolledwindow when the selection goes off the screen.
My idea to get around this is when the down button is pressed for the selection to move down one (as it already does) and then for the scrolledwindow to scroll to the selection on treeview but I'm unable to work out how.
I'm using Gtk3
Can anyone give me any ideas?
See: http://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeView.html#Gtk.TreeView.scroll_to_cell
Do not add you treeview to the scrolled window with "add_with_viewport". See http://mailman.daa.com.au/cgi-bin/pipermail/pygtk/2009-January/016440.html
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Auto Scroll")
self.set_size_request(400, 200)
self.liststore = Gtk.ListStore(str, str)
for n in range(40):
self.liststore.append(["Info", "http://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeView.html"])
treeview = Gtk.TreeView(model=self.liststore)
for n, name in enumerate(["Name", "Link"]):
renderer_text = Gtk.CellRendererText()
column_text = Gtk.TreeViewColumn("Text", renderer_text, text=n)
treeview.append_column(column_text)
scrolled_window = Gtk.ScrolledWindow()
self.add(scrolled_window)
scrolled_window.add(treeview)
def main(self):
Gtk.main
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
After you've moved the selection call gtk_tree_view_scroll_to_cell on the selected path.
from gi.repository import Gtk, Gdk
def drag_data_get_cb(widget, drag_context, selection_data, info, time):
print selection_data.get_data_type()
print widget.get_text()
return widget.get_text()
def drag_begin_cb(widget, dragcontext):
print dragcontext, widget
return dragcontext
label = Gtk.Label()
label.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [], Gdk.DragAction.COPY)
label.set_text("Drag Me!")
label.connect("drag_data_get", drag_data_get_cb)
label.connect("drag_begin", drag_begin_cb)
window = Gtk.Window()
window.add(label)
window.connect("delete_event", Gtk.main_quit)
window.set_default_size(300, 250)
window.show_all()
Gtk.main()
ive been hitting my head against a wall over this for a few days now,
can anyone tell me why this doesnt allow me to drag text into other widgets? neither of the drag events fire at all
It says in this tutorial that you cannot use widgets without windows, such as Gtk.Label as drag and drop sources. You can replace the label with a button for instance:
label = Gtk.Button.new_with_label("Drag Me!")
in order for this example to work.
I am trying to get an image to appear next to a menu item but it isn't working.
In order to make this as simple as possible, I have created a very simple example below that highlights the problem:
import pygtk
pygtk.require('2.0')
import gtk
class MenuExample:
def __init__(self):
window = gtk.Window()
window.set_size_request(200, 100)
window.connect("delete_event", lambda w,e: gtk.main_quit())
menu = gtk.Menu()
menu_item = gtk.ImageMenuItem("Refresh")
img = gtk.image_new_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_MENU)
img.show()
menu_item.set_image(img)
menu.append(menu_item)
menu_item.show()
root_menu = gtk.MenuItem("File")
root_menu.show()
root_menu.set_submenu(menu)
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
menu_bar = gtk.MenuBar()
vbox.pack_start(menu_bar, False, False, 2)
menu_bar.show()
menu_bar.append(root_menu)
window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
MenuExample()
main()
When I run the application, it shows the menu item, but it does not show the image next to it.
OS: Ubuntu 10.04 64-bit
Python version: 2.6.5
Hmmm... it turns out the answer was that my desktop theme had disabled icons for menus. (Who knows why.)
After enabling the option, the icons now show up.