Python XRC Buttons not clickable - python

I'm working on a python app, and have hit a snag. My goal is to have a main frame containing numerous controls, and a panel which will be loaded from an xrc file specified at run time. Essentially, when a user clicks a button, I want a panel to be populated from an xrc file, the xrc file is chosen based on which button is clicked.
I'm defining the UI with XRCed, and here's what I have so far. This seems to load the sub-panel controls into the frame, but they aren't clickable, and they don't appear within the DynamicPanel as I hoped they would. Your help would be greatly appreciated.
EDIT
I've revised my main code, and now the controls appear where they in the frame, however the buttons still aren't clickable. Can anyone tell me why?
Python code:
import wx
import wx.xrc as xrc
from UNTITLED_xrc import xrcFRAME1
class XrcFrameSubClass(xrcFRAME1):
""""""
#----------------------------------------------------------------------
def __init__(self):
xrcFRAME1.__init__(self, parent = None)
self.Bind(wx.EVT_BUTTON, self.OnButton_1, self.button1)
self.Bind(wx.EVT_BUTTON, self.OnButton_2, self.button2)
self.Bind(wx.EVT_BUTTON, self.OnButton_Clear, self.Button_Clear)
self.Show()
def OnButton_Clear(self, event):
self.ClearSubPanel()
def ClearSubPanel(self):
try:
self.SubPanel.Destroy()
self.Fit()
self.Layout()
except:
pass
def OnButton_1(self, event):
self.ClearSubPanel()
SubPanel_xrc = '''<?xml version="1.0" ?><resource>
<object class="wxPanel" name="SubPanel">
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="wxStaticText">
<label>Your Mother was a</label>
</object>
</object>
<object class="sizeritem">
<object class="wxButton" name="SubButton1">
<label>Hamster</label>
</object>
</object>
<object class="sizeritem">
<object class="wxButton" name="SubButton2">
<label>Black Knight</label>
</object>
</object>
</object>
</object>
</resource>'''
#PanelCoord = self.DynamicPanel.GetPosition()
TopSizer = self.TopPanel.GetSizer()
res = xrc.EmptyXmlResource() #Here it is
res.LoadFromString(SubPanel_xrc)
self.SubPanel = res.LoadPanel(self, "SubPanel")
#Note: the first argument of the sizer.Insert() method is the index at which to insert the new item
TopSizer.Insert(1,self.SubPanel,1, wx.EXPAND|wx.ALL, 5)
self.Fit()
self.Layout()
# #Not sure how to do the event binding dynamically yet
# self.SubButton1 = xrc.XRCCTRL(self.SubPanel, "SubButton1")
# self.SubButton2 = xrc.XRCCTRL(self.SubPanel, "SubButton2")
def OnButton_2(self, event):
self.ClearSubPanel()
SubPanel_xrc = '''<?xml version="1.0" ?>
<resource>
<object class="wxPanel" name="SubPanel">
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxStaticText">
<label>Your father smelt of</label>
</object>
</object>
<object class="sizeritem">
<object class="wxButton" name="SubButton1">
<label>Elderberries</label>
</object>
</object>
<object class="sizeritem">
<object class="wxButton" name="SubButton2">
<label>Shrubberies</label>
</object>
</object>
</object>
</object>
</resource>'''
TopSizer = self.TopPanel.GetSizer()
res = xrc.EmptyXmlResource() #Here it is
res.LoadFromString(SubPanel_xrc)
self.SubPanel = res.LoadPanel(self, "SubPanel")
#Note: the first argument of the sizer.Insert() method is the index at which to insert the new item
TopSizer.Insert(1,self.SubPanel,1, wx.EXPAND|wx.ALL, 5)
self.Layout()
self.Fit()
# self.SubButton1 = xrc.XRCCTRL(self.SubPanel, "SubButton1")
# self.SubButton2 = xrc.XRCCTRL(self.SubPanel, "SubButton2")
if __name__ == "__main__":
app = wx.App(False)
frame = XrcFrameSubClass()
app.MainLoop()
MainFrame:
<?xml version="1.0" encoding="cp1252"?>
<resource>
<object class="wxFrame" name="FRAME1">
<title>This is the title</title>
<object class="wxPanel" name="TopPanel">
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="wxButton" name="button1">
<label>Load SubXRC 1</label>
</object>
<option>1</option>
<flag>wxGROW</flag>
</object>
<object class="sizeritem">
<object class="wxButton" name="button2">
<label>Load SubXRC 2</label>
</object>
<option>1</option>
<flag>wxGROW</flag>
</object>
<object class="sizeritem">
<object class="wxButton" name="Button_Clear">
<label>Clear</label>
</object>
</object>
<object class="spacer">
<size>40,20</size>
</object>
</object>
</object>
<object class="sizeritem">
<object class="wxPanel" name="DynamicPanel"/>
<option>1</option>
<flag>wxEXPAND</flag>
<minsize>100,200</minsize>
</object>
<object class="sizeritem">
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxButton" name="button3">
<label>button3</label>
</object>
</object>
<object class="sizeritem">
<object class="wxButton" name="button4">
<label>button4</label>
</object>
</object>
</object>
<flag>wxALIGN_CENTRE</flag>
</object>
</object>
</object>
</object>
</resource>

Related

PyGObject cannot recognize hdy.window

Note: I unfortunately use Google translate for translation. Please ignore my mistakes.
Hello there. I'm trying to develop an application with PyGTK. I am using Glade as UI designer. But unfortunately when I try to start my application I get an error like this.
[alperen#fedora merhaba-dunya]$ python main.py
Traceback (most recent call last):
File "/home/alperen/Projeler/merhaba-dunya/main.py", line 19, in do_activate
self.window = MainWindow(self) # MainWindow nesnemizi oluşturduk.
File "/home/alperen/Projeler/merhaba-dunya/MainWindow.py", line 19, in __init__
self.builder.add_from_file("Example.ui")
gi.repository.GLib.Error: gtk-builder-error-quark: Example.ui:7:1 Invalid object type 'HdyWindow' (6)
Example.ui file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<requires lib="libhandy" version="0.0"/>
<object class="HdyWindow">
<property name="can-focus">False</property>
<child>
<object class="HdyWindowHandle">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-left">15</property>
<property name="margin-right">15</property>
<property name="margin-start">15</property>
<property name="margin-end">15</property>
<property name="margin-top">15</property>
<property name="margin-bottom">15</property>
<property name="orientation">vertical</property>
<property name="spacing">13</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Selamlar!</property>
<property name="ellipsize">middle</property>
<attributes>
<attribute name="scale" value="2"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkButton" id="tikla">
<property name="label" translatable="yes">Bana tıkla!</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<signal name="clicked" handler="on_tikla_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="kapat">
<property name="label" translatable="yes">Uygulamayı kapat</property>
<property name="width-request">71</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="halign">end</property>
<property name="hexpand">True</property>
<signal name="clicked" handler="on_kapat_clicked" swapped="no"/>
<style>
<class name="destructive-action"/>
</style>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Bu bir örnek yazı
Bu alt
alta yazılmış bir yazı
Burası yazılarla dolu!</property>
<property name="justify">fill</property>
<property name="xalign">0.05999999865889549</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<object class="GtkAboutDialog" id="hakkinda">
<property name="can-focus">False</property>
<property name="title" translatable="yes">Hakkında</property>
<property name="type-hint">dialog</property>
<property name="program-name">Örnek uygulama</property>
<property name="version">0.1</property>
<property name="comments" translatable="yes">GTK+ ile yazılmış merhaba dünya tarzı bir uygulama</property>
<property name="website">github.com/Afacanc38/merhaba-dunya</property>
<property name="website-label" translatable="yes">Kaynak Kodu</property>
<property name="authors">Alperen İsa Nalbant</property>
<property name="logo-icon-name">help-about</property>
<property name="license-type">gpl-3-0</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can-focus">False</property>
<property name="layout-style">end</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
<object class="GtkMessageDialog" id="merhaba">
<property name="can-focus">False</property>
<property name="icon-name">face-smile</property>
<property name="type-hint">dialog</property>
<property name="buttons">close</property>
<property name="text" translatable="yes">Merhaba
Dünya</property>
<property name="secondary-text" translatable="yes">hemele hümele</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can-focus">False</property>
<property name="homogeneous">True</property>
<property name="layout-style">expand</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
main.py file:
#!/usr/bin/env python3
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, Gio
from MainWindow import MainWindow
class Uygulama(Gtk.Application):
def __init__(self, *args, **kwargs):
super().__init__(*args,
application_id="org.yapboz.example",
flags=Gio.ApplicationFlags.FLAGS_NONE,
**kwargs)
def do_activate(self):
self.window = MainWindow(self)
app = Uygulama()
app.run(sys.argv)
MainWindow.py file:
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Handy', '1')
from gi.repository import Gtk, GLib, Gio, Gdk
from gi.repository import Handy
# CSS dosyamızı ekleyelim.
screen = Gdk.Screen.get_default()
provider = Gtk.CssProvider()
provider.load_from_path("style.css")
Gtk.StyleContext.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
class MainWindow:
def __init__(self, app):
self.builder = Gtk.Builder()
self.builder.add_from_file("Example.ui")
self.builder.connect_signals(self)
self.window = self.builder.get_object("window")
self.dialog_about = self.builder.get_object("hakkinda")
self.merhaba = self.builder.get_object("merhaba")
self.window.set_application(app)
self.window.set_icon_from_file('images/logo.png')
self.window.show_all()
def on_tikla_clicked(self, button):
self.merhaba.run()
self.merhaba.hide()
def on_kapat_clicked(self, button):
self.window.get_application().quit()
def on_about_clicked(self, button):
self.dialog_about.run()
self.dialog_about.hide()
Libhandy needs to be initialized before it can be recognized in UI files. Try adding Handy.init() in your main file.

How to disable GtkAssistant side bar with Page Title defined?

I'm struggling with a GtkAssistant sidebar that is appearing automatically when I define a page title.
GtkAssistant does not accept Window Title. You need to define a Page Title but it enables that side bar.
I just want the Page Title. An example:
Considering this example, I would like to keep "Error Details" title but hide side bar. It is not useful for me. And there is no Gtk Doc mentions to remove that. Not sure if I can.
A glade example:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.2"/>
<object class="GtkAssistant" id="assistant">
<property name="can_focus">False</property>
<property name="use_header_bar">0</property>
<signal name="cancel" handler="on_destroy" swapped="no"/>
<signal name="close" handler="on_destroy" swapped="no"/>
<signal name="delete-event" handler="on_destroy" swapped="no"/>
<child>
<object class="GtkBox" id="page1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">7</property>
<property name="margin_right">7</property>
<property name="margin_top">7</property>
<property name="margin_bottom">7</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Name:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Surname:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="page_type">intro</property>
<property name="title" translatable="yes">User Info</property>
<property name="has_padding">False</property>
</packing>
</child>
<child>
<object class="GtkBox" id="page2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">7</property>
<property name="margin_right">7</property>
<property name="margin_top">7</property>
<property name="margin_bottom">7</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Address:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Telephone:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="title" translatable="yes">Extra Info</property>
<property name="has_padding">False</property>
</packing>
</child>
<child>
<object class="GtkBox" id="page3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Thanks for registering!</property>
<attributes>
<attribute name="weight" value="bold"/>
<attribute name="scale" value="1.5"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="page_type">summary</property>
<property name="title" translatable="yes">Success</property>
<property name="has_padding">False</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</interface>
And the result is:
I would like to remove this weird side bar.
Question: How to disable GtkAssistant side bar with Page Title defined?
Using your glade.xml, i get the following error:
(Assistant.py:4515): Gtk-CRITICAL **: gtk_assistant_set_page_has_padding: assertion 'child != NULL' failed
and the Next Button keep stay insensitive.
Solved: Using assistant.set_page_complete(box, True) enables the Next Button.
However, using the following API Reference I get this result:
PyGObject API Reference:
Gtk.Assistant.signals.prepare
Gtk.Assistant.set_page_complete
Gtk.HeaderBar
Drawback:
The OS see the Gtk.Assistant. window title as undefined!
Using GtkAssistant.set_title(... has no result!
First add the signal "prepare" using Glade in GtkAssistant.
<signal name="prepare" handler="on_assistant_prepare" swapped="no"/>
Usage:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class GtkAssistant:
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file(__file__.replace('.py', '.glade'))
self.builder.connect_signals(self)
assistant = self.builder.get_object("assistant")
# Preserve the page titels from glade
# Set page titles to empty '' to force not to show sidebar
for _id in ["page1", "page2", "page3"]:
box = self.builder.get_object(_id)
box.page_title = assistant.get_page_title(box)
assistant.set_page_title(box, '')
# Gtk.Assistant refuses to do .set_title even defined in glade
# <property name="title" translatable="yes">Registration-Assistant</property>
assistant.set_title('Registration-Assistant')
# Replace assistant.set_page_title(... with own function
assistant.set_page_title = self.set_page_title
# Replace the window decoration with a custom HeaderBar
self.title = Gtk.Label()
headerbar = Gtk.HeaderBar()
headerbar.set_show_close_button(True)
headerbar.set_custom_title(self.title)
assistant.set_titlebar(headerbar)
assistant.show_all()
def set_page_title(self, box, title):
self.title.set_text(title)
def on_assistant_prepare(self, assistant, box, user_data=None):
assistant.set_page_title(box, box.page_title)
# This will make update the buttons state
# to be able to continue the task.
assistant.set_page_complete(box, True)
def on_destroy(self, widget, data=None):
Gtk.main_quit()
if __name__ == '__main__':
assist = GtkAssistant()
Gtk.main()
Tested with Python: 3.5 - gi.__version__: 3.22.0 - Glade 3.20.0

Gtk3 - Python - Glade - 'Linked' comboboxes on different windows

I have two Gtk.Comboboxes on a main window :
the first, has its own Gtk.Liststore
the second one, linked to a Gtk.TreeModelFilter which is linked to a second Gtk.Liststore
When I select a value in the first one, the second one displays values according to my selection. This works well in the main window.
But in my application, I need to use this comboboxes combination many times, and when I use it on another window, this generates a Gtk Error that I don't understand:
"Gtk-CRITICAL **: 00:27:38.289:
gtk_tree_model_filter_set_visible_func: assertion
'filter->priv->visible_method_set == FALSE' failed"
Could someone explain me what's wrong with my code and what is the meaning of that error message?
Here is the glade file (test.ui)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name id -->
<column type="gchararray"/>
<!-- column-name name -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">1</col>
<col id="1" translatable="yes">fruits</col>
</row>
<row>
<col id="0" translatable="yes">2</col>
<col id="1" translatable="yes">colors</col>
</row>
</data>
</object>
<object class="GtkListStore" id="liststore2">
<columns>
<!-- column-name id -->
<column type="gchararray"/>
<!-- column-name name -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">10</col>
<col id="1" translatable="yes">lemon</col>
</row>
<row>
<col id="0" translatable="yes">11</col>
<col id="1" translatable="yes">apple</col>
</row>
<row>
<col id="0" translatable="yes">12</col>
<col id="1" translatable="yes">strawberry</col>
</row>
<row>
<col id="0" translatable="yes">20</col>
<col id="1" translatable="yes">blue</col>
</row>
<row>
<col id="0" translatable="yes">21</col>
<col id="1" translatable="yes">yellow</col>
</row>
<row>
<col id="0" translatable="yes">22</col>
<col id="1" translatable="yes">purple</col>
</row>
</data>
</object>
<object class="GtkTreeModelFilter" id="filter">
<property name="child_model">liststore2</property>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<signal name="destroy" handler="on_quit" swapped="no"/>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">20</property>
<child>
<object class="GtkToolbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Open other</property>
<property name="use_underline">True</property>
<signal name="clicked" handler="on_other" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">20</property>
<property name="margin_right">20</property>
<property name="row_spacing">5</property>
<property name="column_spacing">10</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkComboBox" id="w1_combo1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">liststore1</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="w1_combo2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">filter</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo2</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkDialog" id="window2">
<property name="can_focus">False</property>
<property name="type_hint">dialog</property>
<property name="transient_for">window1</property>
<child>
<placeholder/>
</child>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="btn_close">
<property name="label" translatable="yes">Close</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">center</property>
<property name="margin_left">20</property>
<property name="margin_right">20</property>
<property name="margin_top">20</property>
<property name="margin_bottom">20</property>
<property name="vexpand">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">center</property>
<property name="margin_top">20</property>
<property name="margin_bottom">20</property>
<property name="vexpand">True</property>
<property name="row_spacing">5</property>
<property name="column_spacing">10</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkComboBox" id="w2_combo1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">liststore1</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="w2_combo2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">filter</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo2</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-7">btn_close</action-widget>
</action-widgets>
</object>
</interface>
and the python file (test.py):
#!/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
import sys
class Main:
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file('test.ui')
self.builder.connect_signals(self)
self.obj = self.builder.get_object
self.window = self.obj('window1')
self.combo1 = self.obj('w1_combo1')
self.combo2 = self.obj('w1_combo2')
self.combo1.connect('changed', self.on_select, self.combo2)
self.filter = self.obj('filter')
self.filter.set_visible_func(self.filter_func)
self.combo1_val = None
self.window.show()
def on_other(self, btn):
Other(self.obj)
def on_select(self, combo, next_combo):
self.combo1_val = combo.get_active_id()
self.filter.refilter()
next_combo.set_active(0)
def filter_func(self, model, iter, data):
if self.combo1_val is None: return True
else: return self.combo1_val == model[iter][0][:1]
def on_quit(self, widget):
Gtk.main_quit()
class Other:
def __init__(self, obj):
self.obj = obj
self.window = self.obj('window2')
self.combo1 = self.obj('w2_combo1')
self.combo2 = self.obj('w2_combo2')
self.combo1.connect('changed', self.on_select, self.combo2)
self.filter = self.obj('filter')
self.filter.set_visible_func(self.filter_func)
self.btn_close = self.obj('btn_close')
self.btn_close.connect('clicked', self.on_hide)
self.combo1_val = None
self.window.show()
def on_select(self, combo, next_combo):
self.combo1_val = combo.get_active_id()
self.filter.refilter()
next_combo.set_active(0)
def filter_func(self, model, iter, data):
if self.combo1_val is None: return True
else: return self.combo1_val == model[iter][0][:1]
self.window.show()
def on_hide(self, btn):
self.window.hide()
def main():
app = Main()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
Thank you in advance.
The problem was due to the fact that the second combobox on the main window and the second combobox on 'other' window were using the same filter.
I've created another filter and the second comboboxes have now their own filter linked to a common Gtk.Liststore.
Everything works fine, now.
Here are the corrected files :
The glade one :
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name id -->
<column type="gchararray"/>
<!-- column-name name -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">1</col>
<col id="1" translatable="yes">fruits</col>
</row>
<row>
<col id="0" translatable="yes">2</col>
<col id="1" translatable="yes">colors</col>
</row>
</data>
</object>
<object class="GtkListStore" id="liststore2">
<columns>
<!-- column-name id -->
<column type="gchararray"/>
<!-- column-name name -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">10</col>
<col id="1" translatable="yes">lemon</col>
</row>
<row>
<col id="0" translatable="yes">11</col>
<col id="1" translatable="yes">apple</col>
</row>
<row>
<col id="0" translatable="yes">12</col>
<col id="1" translatable="yes">strawberry</col>
</row>
<row>
<col id="0" translatable="yes">20</col>
<col id="1" translatable="yes">blue</col>
</row>
<row>
<col id="0" translatable="yes">21</col>
<col id="1" translatable="yes">yellow</col>
</row>
<row>
<col id="0" translatable="yes">22</col>
<col id="1" translatable="yes">purple</col>
</row>
</data>
</object>
<object class="GtkTreeModelFilter" id="w1_filter">
<property name="child_model">liststore2</property>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<signal name="destroy" handler="on_quit" swapped="no"/>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">20</property>
<child>
<object class="GtkToolbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkToolButton">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Open other</property>
<property name="use_underline">True</property>
<signal name="clicked" handler="on_other" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">20</property>
<property name="margin_right">20</property>
<property name="row_spacing">5</property>
<property name="column_spacing">10</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkComboBox" id="w1_combo1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">liststore1</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="w1_combo2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">w1_filter</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo2</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkTreeModelFilter" id="w2_filter">
<property name="child_model">liststore2</property>
</object>
<object class="GtkDialog" id="window2">
<property name="can_focus">False</property>
<property name="type_hint">dialog</property>
<property name="transient_for">window1</property>
<child>
<placeholder/>
</child>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="btn_close">
<property name="label" translatable="yes">Close</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">center</property>
<property name="margin_left">20</property>
<property name="margin_right">20</property>
<property name="margin_top">20</property>
<property name="margin_bottom">20</property>
<property name="vexpand">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">center</property>
<property name="margin_top">20</property>
<property name="margin_bottom">20</property>
<property name="vexpand">True</property>
<property name="row_spacing">5</property>
<property name="column_spacing">10</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkComboBox" id="w2_combo1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">liststore1</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="w2_combo2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">w2_filter</property>
<property name="active">0</property>
<property name="id_column">0</property>
<property name="active_id">0</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo1</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Combo2</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-7">btn_close</action-widget>
</action-widgets>
</object>
</interface>
And the python one :
#!/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
import sys
class Main:
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file('test.ui')
self.builder.connect_signals(self)
self.obj = self.builder.get_object
self.window = self.obj('window1')
self.combo1 = self.obj('w1_combo1')
self.combo2 = self.obj('w1_combo2')
self.combo1.connect('changed', self.on_select, self.combo2)
self.filter = self.obj('w1_filter')
self.filter.set_visible_func(self.filter_func)
self.combo1_val = None
self.window.show()
def on_other(self, btn):
Other(self.obj)
def on_select(self, combo, next_combo):
self.combo1_val = combo.get_active_id()
self.filter.refilter()
next_combo.set_active(0)
def filter_func(self, model, iter, data):
#print(self.combo1_val, model[iter][0][:1])
if self.combo1_val is None: return True
else: return self.combo1_val == model[iter][0][:1]
def on_quit(self, widget):
Gtk.main_quit()
class Other:
def __init__(self, obj):
self.obj = obj
self.window = self.obj('window2')
self.combo1 = self.obj('w2_combo1')
self.combo2 = self.obj('w2_combo2')
self.combo1.connect('changed', self.on_select, self.combo2)
self.filter = self.obj('w2_filter')
self.filter.set_visible_func(self.filter_func)
self.btn_close = self.obj('btn_close')
self.btn_close.connect('clicked', self.on_hide)
self.combo1_val = None
self.window.show()
def on_select(self, combo, next_combo):
self.combo1_val = combo.get_active_id()
self.filter.refilter()
next_combo.set_active(0)
def filter_func(self, model, iter, data):
#print(self.combo1_val, model[iter][0][:1])
if self.combo1_val is None: return True
else: return self.combo1_val == model[iter][0][:1]
self.window.show()
def on_hide(self, btn):
self.window.hide()
def main():
app = Main()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())

Python/Glade/PyGTK - Download Percentage in Glade Label

I'm trying to setup a little download application using Glade, PyGTK, and Python. I have it planned how I'd like to script this progress bar and all, though I feel I'm going about it the wrong way as... Well... It's not working.
I'm using a little script I found for a checking the percentage of a download and then usually it would print to terminal, but instead I'd like it alter a label in Glade. Though, instead of the label changing when as the download progresses, the GUI freezes until the file is done and THEN updates saying it's 100% complete.
What would be the best method about making something like this work? Any help is appreciated, thanks in advance!
Python Script:
import sys
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import urllib2
import time
class FileSelector:
def __init__(self):
#Set the Glade file
filename = "FileSelector.glade"
self.builder = gtk.Builder()
self.builder.add_from_file(filename)
self.builder.connect_signals(self)
self.window = self.builder.get_object("MainWindow")
self.label1 = self.builder.get_object("label1")
def on_MainWindow_destroy(self, obj):
gtk.main_quit() #make the program quit
def btnSubmit_clicked(self, widget):
status_dropbox = self.builder.get_object("chkboxDropbox")
status_python = self.builder.get_object("chkboxPython")
status_chrome = self.builder.get_object("chkboxChrome")
#Start downloader script
url = "http://dl.dropbox.com/u/9235267/project.png"
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
self.label1.set_text(status)
f.close()
if __name__ == "__main__":
FileSelector()
gtk.main()
Glade File (FileSelector.glade):
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="MainWindow">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">MainWindow</property>
<property name="resizable">False</property>
<property name="window_position">center</property>
<signal name="destroy" handler="on_MainWindow_destroy" swapped="no"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Enviroment Downloader</property>
<attributes>
<attribute name="style" value="normal"/>
<attribute name="size" value="300"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxDropbox">
<property name="label" translatable="yes">Dropbox</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxPython">
<property name="label" translatable="yes">Python</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxChrome">
<property name="label" translatable="yes">Google Chrome</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnSubmit">
<property name="label" translatable="yes">Download/Run</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="btnSubmit_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
After the line
self.label1.set_text(status)
Add:
while gtk.events_pending():
gtk.main_iteration()
This will update the GUI and run the main loop while you are waiting for the file to download, and you won't have to use threads.
You will need to use threads for this. If you are downloading in the callback function then the eventing is blocked on the button & you can see that the button is in pressed state i.e. it is waiting for the callback function to complete and return. Although my knowledge of python is nil, I could cook up some sample as follows using google etc:
import sys
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import urllib2
import time
import threading
import glib
# Replace this with what you want to do
def download(label):
index = 0
while True:
if index>1200:
break
index += 120
time.sleep(1)
status = r"%10d " % (index)
label.set_label(status)
class FileSelector:
def __init__(self):
#Set the Glade file
filename = "FileSelector.glade"
self.builder = gtk.Builder()
self.builder.add_from_file(filename)
self.builder.connect_signals(self)
self.window = self.builder.get_object("MainWindow")
self.label1 = self.builder.get_object("label1")
def on_MainWindow_destroy(self, obj):
gtk.main_quit() #make the program quit
def btnSubmit_clicked(self, widget):
status_dropbox = self.builder.get_object("chkboxDropbox")
status_python = self.builder.get_object("chkboxPython")
status_chrome = self.builder.get_object("chkboxChrome")
# Launch the thread to update label
threading.Thread(target=download, args=(self.builder.get_object("label1"),)).start()
if __name__ == "__main__":
FileSelector()
glib.threads_init()
# Add gdk thread enter and leave
gtk.main()
You can refer this question as well.
Hope this helps!
PS: Add thread safety & maybe disable button till download is complete?

problem implementing get_filename() method for GtkFileChooser Button in glade and python?

As a newbee I am trying to create a basic theme installer using Glade and python. Everything is working fine but i am not able to find anywhere how to use get_filename() method for gtkFileChooserButton.
My Glade File Looks Like this.
<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="main">
<property name="title" translatable="yes">Maininin</property>
<property name="window_position">center</property>
<signal name="destroy" handler="on_main_destroy"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<child>
<object class="GtkLabel" id="INFO">
<property name="visible">True</property>
<property name="label" translatable="yes">Theme</property>
</object>
<packing>
<property name="expand">False</property>
<property name="padding">28</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkHButtonBox" id="hbuttonbox1">
<property name="visible">True</property>
<property name="layout_style">spread</property>
<child>
<object class="GtkFileChooserButton" id="DD">
<property name="visible">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkHButtonBox" id="hbuttonbox2">
<property name="visible">True</property>
<property name="layout_style">spread</property>
<child>
<object class="GtkButton" id="Add">
<property name="label">gtk-add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="on_Add_clicked"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="Quit">
<property name="label">gtk-quit</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="on_Quit_clicked"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="WARN">
<property name="visible">True</property>
<property name="label" translatable="yes">Warning</property>
</object>
<packing>
<property name="expand">False</property>
<property name="padding">37</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
`
And Python Code is:
import sys
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
except:
print("GTK Not Availible")
sys.exit(1)
class workplz(object):
def __init__( self ):
builder = gtk.Builder()
builder.add_from_file("heme.glade")
builder.connect_signals({"on_main_destroy" : self.quit, "on_Add_clicked" : self.add, "on_Quit_clicked" : self.quit})
self._window = builder.get_object("main")
self._window.show()
def add(self, widget):
target=DD.get_filename()
print target
def quit(self, widget):
sys.exit(0)
if __name__ == "__main__":
wine = workplz()
gtk.main()
what is the right way of doing it???
I believe if you would get your GtkFileChooserButton object from the builder and save a reference to it as a member of workplz class you could query it in the add signal handler. See if script below would work for you:
class workplz(object):
def __init__( self ):
builder = gtk.Builder()
builder.add_from_file("heme.glade")
builder.connect_signals({"on_main_destroy" : self.quit, "on_Add_clicked" : self.add, "on_Quit_clicked" : self.quit})
# get GtkFileChooserButton object
self.DD = builder.get_object('DD')
self._window = builder.get_object("main")
self._window.show()
def add(self, widget):
target = self.DD.get_filename()
print target
def quit(self, widget):
sys.exit(0)
hope this helps, regards

Categories

Resources