Op 19-08-15 om 01:08 schreef Eric Kelly:
I am a beginner with Python and would like to write a program that includes
a GUI to run it.  I've been through a tutorial on using Python but I'm
trying to also use Gtk and Glade to make the GUI.  I've tried to use the
docs and other tutorials but alas I'm still stuck.

The problem is simply to get the text from a combotext object.  I
simplified the program by only including the combo object and a "Run"
button.  The user should be able to choose a value from the list or enter a
different value manually.  When I click the "Run" button to print the combo
text, I think I get the memory address.  Here is the message that appears
when I click Run:

"<ComboBoxText object at 0x26a9d78 (GtkComboBoxText at 0x41ae178)>"
You are printing the actual Gtk.ComboBoxText class. Take this simple pure Python example:

>>> class Foo: pass
...
>>> f = Foo()
>>> print(f)
<__main__.Foo object at 0x7f3f32e31630>

See the similarity?

You will have to call the get_active_text() method on the Gtk.ComboBoxText class to get the selected text.

API docs: http://lazka.github.io/pgi-docs/Gtk-3.0/classes/ComboBoxText.html#Gtk.ComboBoxText.get_active_text Tutorial: http://learngtk.org/tutorials/python_gtk3_tutorial/html/comboboxtext.html

So your code becomes:

    def on_buttonRun_clicked(self,widget):
        comboText = self.builder.get_object('comboboxtext1')
        print(comboText.get_active_text())

Here are the full Gtk API docs: http://lazka.github.io/pgi-docs/index.html#Gtk-3.0 And here the tutorial: http://learngtk.org/tutorials/python_gtk3_tutorial/html/

Timo


I realize that the program does not know what part of the object to get,
but I am unclear about how to tell it where the text is.  I've tried
assigning variable names to what I think are the appropriate user data
values, but of course none worked.  I'm using Glade 3, Gtk+ 3, and Python
34.

Thanks in advance for any help and suggestions (I'm sure there are other
mistakes here too).

Eric


The Python code is here:
--------------------------------------------------------------------
#!C:\Python34
from gi.repository import Gtk


# Make a window to control the program
class MyGI(Gtk.Window):

     def __init__(self):
         Gtk.Window.__init__(self,title='Title')
         self.builder = Gtk.Builder()
         self.builder.add_from_file('GI_test.glade')

         # Define handlers for signals from window
         handlersDict = {
             'on_applicationwindow1_destroy':Gtk.main_quit,
             'on_buttonRun_clicked':self.on_buttonRun_clicked
             }

         # Get the objects from the window
         self.window = self.builder.get_object('applicationwindow1')
         self.buttonRun = self.builder.get_object('buttonRun')

         # Connect the signals with their handlers
         self.builder.connect_signals(handlersDict)


     def on_buttonRun_clicked(self,widget):
         comboText = self.builder.get_object('comboboxtext1')
         print(comboText)


def main():
     win = MyGI()
     Gtk.main()

if __name__ == '__main__':
     main()



The XML code is here:
-----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
   <requires lib="gtk+" version="3.10"/>
   <object class="GtkApplicationWindow" id="applicationwindow1">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
     <signal name="destroy" handler="on_applicationwindow1_destroy"
swapped="no"/>
     <child>
       <object class="GtkBox" id="box1">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="orientation">vertical</property>
         <child>
           <object class="GtkComboBoxText" id="comboboxtext1">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
             <property name="margin_bottom">5</property>
             <items>
               <item translatable="yes">0</item>
               <item translatable="yes">100</item>
               <item translatable="yes">200</item>
             </items>
           </object>
           <packing>
             <property name="expand">False</property>
             <property name="fill">True</property>
             <property name="position">0</property>
           </packing>
         </child>
         <child>
           <object class="GtkButton" id="buttonRun">
             <property name="label" translatable="yes">Run</property>
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <property name="receives_default">True</property>
             <signal name="clicked" handler="on_buttonRun_clicked"
swapped="no"/>
           </object>
           <packing>
             <property name="expand">False</property>
             <property name="fill">True</property>
             <property name="position">1</property>
           </packing>
         </child>
       </object>
     </child>
   </object>
</interface>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to