Search code examples
pythonpython-2.7gtkglade

How to pass a string from a text box to a function


I am writing a basic gui for my log parsing script. Im using GTK 2x, Glade and Python 2.7.3.

I have some global variables defined and need to change those according the user input in the gui text boxes. The whole layout is made in glade. Any tips how to pass strings and values from widgets to functions outside the GUI class?

Sample text box definition from a glade file:

<child>
      <object class="GtkEntry" id="serverIDtext">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="invisible_char">●</property>
        <property name="text" translatable="yes">Server name.</property>
        <property name="primary_icon_activatable">False</property>
        <property name="secondary_icon_activatable">False</property>
        <property name="primary_icon_sensitive">True</property>
        <property name="secondary_icon_sensitive">True</property>
      </object>
      <packing>
        <property name="top_attach">2</property>
        <property name="bottom_attach">3</property>
      </packing>
    </child>

GUI class:

import pygtk
import gtk
import gtk.glade

from pyParse2 import *

class GUI:
  def __init__(self):    
    builder = gtk.Builder()
    builder.add_from_file("parsergui.glade")
    self.window = builder.get_object("window1")
    builder.connect_signals(self)

  def on_window_destroy(self, widget, data = None):
    gtk.main_quit()

  def parseButton(self, widget, data = None):
    parseLaunch()

EDIT as per MG. advice: I tried your first advice and prepared something like this:

import pygtk
import gtk
import gtk.glade

from pyParse2 import *

class GUI:
  def __init__(self):    
    builder = gtk.Builder()
    builder.add_from_file("parsergui.glade")
    self.window = builder.get_object("window1")
    builder.connect_signals(self)
    self.sID = builder.get_object("serverIDtext")

  def on_window_destroy(self, widget, data = None):
    gtk.main_quit()

  def parseButton(self, widget, data = None):
    global serverID
    serverID = self.sID
    print serverID
    parseLaunch()

And got this:

<gtk.Entry object at 0x2a97a58 (GtkEntry at 0x20e9118)>

As I understand it (and I am most probably wrong), I cant use get_text, but have to do get_object, which doesnt return a string. Is there a way to go around this?

O_O


Solution

  • You can access the entry by the Builder class as you do with the window.

    # in __init__
    self.entry = builder.get_object('serverIDtext')
    

    There is an alternative but a cause of a bug or a misconception on how it works, or how it should work, requires a bit of tests to make it work. Basically you can pass the reference of the text box to the handler via the User data parameter. In glade when you associate the parseButton handler to a signal, set the User data to the text box. Now the tricky part: I don't remember if the text box is passed as widget parameter or data parameter and the Swap checkbox control this behavior.

    When you have a reference to the text box you can easily retrieve the string calling the get_text() method.