GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Jul 25, 2014 12:38 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: PyGTK Accepting User Input
PostPosted: Fri Oct 11, 2013 7:51 pm 
Offline

Joined: Fri Oct 11, 2013 12:51 pm
Posts: 1
Hi,

I'm learning GTK+ and have played with the examples with Vala and C, but have opted for Python and PyGTK.

I have browsed through the PyGTK 3 tutorial at:

http://python-gtk-3-tutorial.readthedocs.org/en/latest/

But I am struggling. All I would like to to do is have two text boxes where the user enters two numbers and clicks calculate and the result of the two numbers added is shown in a third text box. I know how to do it in raw python:

num1 = input("Please enter in the first number to be added.")
num2 = input("Please enter in the second number to be added.")
sum = num1 + num2
print "The sum of the two numbers entered is: ", sum

Would anybody be able to point me in the right direction or provide me with an example?

Many thanks

Peter


Top
 Profile  
 
 Post subject: Re: PyGTK Accepting User Input
PostPosted: Sat Oct 12, 2013 8:52 am 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
quick and dirty without oo:

Code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk

window=Gtk.Window()
window.connect("destroy",Gtk.main_quit)

grid=Gtk.Grid()
window.add(grid)
grid.set_orientation(Gtk.Orientation.VERTICAL)

entry1=Gtk.Entry()
grid.add(entry1)

entry2=Gtk.Entry()
grid.add(entry2)

label=Gtk.Label()
grid.add(label)


def on_changed(widget):
    text1=entry1.get_text()
    text2=entry2.get_text()
    try:
        x="%d" % (float(text1)+float(text2))
    except:
        x="XX"
    label.set_text(x)

entry1.connect("changed",on_changed)
entry2.connect("changed",on_changed)

window.show_all()
Gtk.main()



Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group