python3 gtk3 GUI only partially updating despite using GObject.idle_add [duplicate] - python

I have been using this website pretty often in order to solve small issues that I have while programming in Python. This time, somehow I could not find a suitable solution for my situation. So, here is my problem:
I want to dynamically add entries to a gtk.VBox widget. The problem is that it doesn't work the way I want it to work. I simply have a button, whose action is to add an additional widget to a VBox. Unfortunately the widget doesn't appear on the window. I guess, I have to add something like a repaint function call, but I didn't find anything like that. Here is a sample code, showing my problem:
import gtk
class DynamicVbox:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.close_application)
self.window.set_size_request(400,320)
#a hBox to put the button and the dynamic vBox
hBox = gtk.HBox(False, 0)
addButton = gtk.Button("add checkbox")
addButton.connect("clicked", self.AddCheckButton)
self.vBox = gtk.VBox(False, 0)
self.vBox.pack_start(gtk.CheckButton("CheckButton"), True, True, 1)
hBox.pack_start(self.vBox, True, True, 5)
hBox.pack_end(addButton, False, False, 5)
self.window.add(hBox)
#start gtk
self.window.show_all()
gtk.main()
def AddCheckButton(self, button):
self.vBox.pack_start(gtk.CheckButton("CheckButton"), True, True, 1)
print "adding checkbox..."
def close_application(self, widget):
gtk.main_quit()
# run it
a = DynamicVbox()
A appreciate any help. Thanks in advance.

The new check button is there, but not visible until you call show() on it:
def AddCheckButton(self, button):
button = gtk.CheckButton("CheckButton")
self.vBox.pack_start(button, True, True, 1)
button.show()
print "adding checkbox..."

Related

Manually toggling of widgets ability to emit signals in pygtk

I'm probably missing something basic in my pygtk programming, but I want to connect a signal to e.g. an gtk.Entry and then make it only emit the connected signal when I allow it to do so.
That is, I want to add something to toggle_signalling in this minimal code (only for interactive use) so that Hello is only printed when signalling is "allowed":
import gtk
signal_states = ['On', 'Off']
signal_state = True
def reporter_function(*args,**kwargs):
print "Hello"
def toggle_signaling(widget, **kwargs):
global signal_state
signal_state = not signal_state
widget.set_label(signal_states[signal_state])
print ['Emit allowed', 'Emit not allowed'][not signal_state]
w = gtk.Window()
e = gtk.Entry()
b = gtk.Button(label=signal_states[signal_state])
hbox = gtk.HBox()
hbox.pack_start(e)
hbox.pack_end(b)
e.connect("changed", reporter_function)
b.connect("clicked", toggle_signaling)
w.add(hbox)
w.show_all()
I've previously let there be a boolean flag for such send signal state e.g. self._updating in my custom widget-classes and let the callback-functions check this state before doing anything. That is not what I want.
I want a gtk-native way of letting the widget know that it shouldn't send the signal (when I've clicked the button in the example). I'm pretty sure I've stumbled upon a way of doing this once but I'm lost at finding it again.
Also, to be absolutely clear, the widget must still be allowed to be enabled.
I don't think there's a way around the boolean flag. Wether a widget is allowed to emit a signal or not is additional application logic and therefore has to be kept somewhere.
Based on your previous research on the topic and the quite acurately described functionality you're most probably looking for chapter 20.1.2 of the PyGTK tutorial.
I put comprehensive example code together. The only thing to keep around except the boolean indicator is the handler_id of the connected signal. As you might notice, it's programmed in Gtk3, but the important methods handler_block and handler_unblock function the exact same way in both Gtk 2 and 3.
from gi.repository import Gtk
class TestWindow(Gtk.Window):
def __init__(self, *args, **kwargs):
Gtk.Window.__init__(self, *args, **kwargs)
self.connect("destroy", Gtk.main_quit)
self.is_allowed = True
self.create_widgets()
self.show_all()
def create_widgets(self):
box = Gtk.HBox()
self.entry = Gtk.Entry()
self.handler_id = self.entry.connect("changed", self.on_entry_changed)
box.pack_start(self.entry, True, True, 0)
button = Gtk.Button("Toggle")
button.connect("clicked", self.on_button_clicked)
box.pack_start(button, True, True, 0)
self.add(box)
def on_entry_changed(self, *args):
print "entry has changed"
def on_button_clicked(self, *args):
if self.is_allowed:
self.entry.handler_block(self.handler_id)
print "now blocking"
else:
self.entry.handler_unblock(self.handler_id)
print "now unblocking"
self.is_allowed = not self.is_allowed
TestWindow()
Gtk.main()

check which button is pressed gtk3 using python

i have 10 button, which correspond to the same method. how am i going to check which button was clicked in the corresponding method? i tried to check for the button press of a particular button in the list by the following code, but i got segmentation fault error:
for i in range(0,10):
if button_list[i].clicked():
break
break
#operation with respect to the button clicked
Here's a sample code that illustrates knowing what button triggered the event by using the label of the button:
from gi.repository import Gtk
class ButtonWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Button Demo")
self.set_border_width(10)
hbox = Gtk.Box(spacing=6)
self.add(hbox)
#Lets create 10 buttons for this demo
#You could create and set the label for
#each of the buttons one by one
#but in this case we just create 10
#and call them Button0 to Button9
for i in range(10):
name = "Button{}".format(i)
button = Gtk.Button(name)
button.connect("clicked", self.on_button_clicked)
hbox.pack_start(button, True, True, 0)
def on_button_clicked(self, button):
print button.get_label()
def on_close_clicked(self, button):
print "Closing application"
Gtk.main_quit()
win = ButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
So you could just check what the label is and act accordingly.
Once you have connected all the buttons to the same callback, I assume the callback will have this signature: callback(button) where button is the button that emitted the clicked signal.
Inside that callback should be easy to check which button was clicked using something like:
button_list.index(button)
This will return the index of the button inside your list.

create a gtk.window under a gtk.widget

I wanna show a gtk.Window under a gtk.widget.
But I don't know how to retrieve the gtk.widget's coordinates for my gtk.window.
Anyone knows ?
Thanks.
You can use the "window" attribute of the gtk.Widget to get the gtk.gdk.Window associated with it. Then look at the get_origin() method to get the screen coordinates.
These coordinates are for the top-level window, I believe (I could be wrong about that, but my code below seems to support that). You can use the get_allocation() method to get the coordinates of a widget relative to its parent.
I got the idea from here. Be warned though: some window managers ignore any initial settings for window position. You might want to look at this post for more info, but I haven't personally checked it out.
Were you intending to create another top-level window? Or a popup window?
#!/usr/bin/env python
import sys
import pygtk
import gtk
class Base:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.on_destroy)
self.box = gtk.VButtonBox()
self.buttons = [
gtk.Button("Test 1"),
gtk.Button("Test 2"),
gtk.Button("Test 3")
]
for button in self.buttons:
self.box.add(button)
button.connect("clicked", self.show_coords)
button.show()
self.window.add(self.box)
self.box.show()
self.window.show()
def show_coords(self, widget, data=None):
print "Window coords:"
print self.window.get_window().get_origin()
print "Button coords:"
print widget.get_allocation()
def on_destroy(self, widget, data=None):
gtk.main_quit()
def main(self):
gtk.main()
if __name__ == "__main__":
base = Base()
base.main()

How to set default button in PyGTK?

I have very simple window where I have 2 buttons - one for cancel, one for apply. How to set the button for apply as default one? (When I press enter, "apply" button is pressed)
However, I want to set focus to the first input widget (I can't use grab_focus() on the button)
Any suggestions?
Edit:
After wuub's answer it works visually good. However, when I press the button in different widget, it doesn't run callback of the default button.
Example code:
import os, sys, pygtk, gtk
def run(button, window):
dialog = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "OK")
dialog.run()
dialog.destroy()
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
vbox = gtk.VBox(spacing = 10)
entry = gtk.Entry()
vbox.pack_start(entry)
button = gtk.Button(stock = gtk.STOCK_SAVE)
button.connect("clicked", run, window)
button.set_flags(gtk.CAN_DEFAULT)
window.set_default(button)
vbox.pack_start(button)
window.add(vbox)
window.show_all()
gtk.main()
EDIT2: Every input which can activate default widget must be ran
widget.set_activates_default(True)
http://www.pygtk.org/docs/pygtk/class-gtkdialog.html#method-gtkdialog--set-default-response
http://www.pygtk.org/docs/pygtk/class-gtkwindow.html#method-gtkwindow--set-default

PyGTK Label emit ButtonPressEvent does nothing and returns False

Labels can have links by using Pango markup, but the "button-press-event" on a label is not emitted when the label is used in an Expander. I thought that if I created a subclass of Expander and emitted the "button-press-event" myself, the link would be clicked. A minimal example of "button-press-event" failing is given below:
import gtk
def on_activate(label, uri):
print(uri)
def on_click(widget, event):
label.emit('button-press-event', event)
window = gtk.Window()
expander = gtk.Expander()
window.add(expander)
label = gtk.Label('link')
label.set_use_markup(True)
label.connect("activate-link", on_activate)
expander.set_label_widget(label)
expander.connect("button-press-event", on_click)
window.connect("destroy", gtk.main_quit)
window.show_all()
gtk.mainloop()
If I add print calls, I can see that on_click() is being called, but on_activate() is not. The strange thing is, if I print the returns of label.emit(...), it prints False. What does that mean? How can I click the link?

Categories