Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44589

Re: in need of some help...

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.python
Subject Re: in need of some help...
Date 2013-05-01 12:15 +0000
Organization Freie Universitaet Berlin
Message-ID <aucfb0F61jrU1@mid.uni-berlin.de> (permalink)
References <1bdc367a-4187-440b-85a8-de69360bc71d@googlegroups.com> <mailman.1201.1367360008.3114.python-list@python.org> <001fe0f1-9124-4cbb-bc84-1b58ee7b8138@googlegroups.com> <mailman.1202.1367363021.3114.python-list@python.org> <47292460-b601-47e7-bdba-ccdc5f0d1c27@googlegroups.com>

Show all headers | View raw


Alex Norton <ayjayn1101@gmail.com> wrote:
> thanks... ill take a look at the Qt event handling

It's rather simple: instead of the program running through a
sequence of steps, the program normally is basically doing
nothing. It just reacts to events that normally come from
the user, i.e. the user clicks on some icon or widget, or
(s)he enters something via the keyboard. You etermine which
of all the possible events to which widget are relevant to
you, write handler functions for them and tell the widget
to call some function when an event happens. The simplest
case is a button: you want to react to it, so you write a
function for what's to be done when it's clicked on and
then tell Qt to call that function once it gets clicked
(there are different events even for a simple push button,
it can be clicked, just pushed, released etc.). And if you
have set up everything for that you tell Qt to start waiting
for events.

So the steps are:

  1. Tell Qt that this is a program using it

     app = QtGui.QApplication( sys.argv )

  2. Create your graphical interface (what you seem to
     have done more or less)

  3. Connect desired events (what's called "signals" in
     Qt lingo) for a certain widget to the function to be
     called with something like

     your_widget.clicked.connect( your_function )

     (replace 'clicked' with e.g. 'pushed' or 'released'
     when interested in a push or release signal instead)

  4. Start the event loop (i.e. have Qt wait for the user
     to do something and call one of your functions if the
     user did something you're interested in) with

     app.exec_( )

     When this returns the game is over.

So you don't wait for keyboard input with input() like in
your original program but instead tell Qt to do the waiting
for you and call the appropriate function you defined when
something interesting happens.

What you probably will have to change about the graphical
interface is that instead of using QLabel widgets for 'Air',
'Earth', 'Fire', 'Water' to use e.g. QPushButtons since
QLabels are rather static objects - they don't receive any
"click" events and it's rather likely some kind of event
like this is what you're going to want to react to. And for
that QPushButtons seem to be the simplest choice to start
with.

So have an 'Air' button (let's call it 'bAir' and then do

   bAir.clicked.connect( air_clicked )

after defining a function air_clicked() in which you deal
with that case. that might be as simple as

def air_clicked( ) :
	# Randomly pick one of 'air', 'fire', 'water' or 'earth'

    z = [ 'air', 'fire', 'water', earth' ][ random.randrange( 4 ) ]

	if z == 'air' :
        print( 'Stalemate' )
    elif z == 'water' :
        print( 'Air removes Water, you win!' )
    ...

Now, when during the game the 'Air' button is clicked this
function will get called.

Of course, it might be nicer to have a "result" label some-
where in the graphical interface which you set to the text
instead of printing it out to the console. And you also will
probably add some "Quit" button to end the game.

                              Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

in need of some help... Alex Norton <ayjayn1101@gmail.com> - 2013-04-30 15:06 -0700
  Re: in need of some help... Chris Angelico <rosuav@gmail.com> - 2013-05-01 08:13 +1000
    Re: in need of some help... Alex Norton <ayjayn1101@gmail.com> - 2013-04-30 15:30 -0700
      Re: in need of some help... Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-30 17:02 -0600
        Re: in need of some help... Alex Norton <ayjayn1101@gmail.com> - 2013-04-30 16:20 -0700
          Re: in need of some help... Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-01 00:01 -0400
          Re: in need of some help... Chris Angelico <rosuav@gmail.com> - 2013-05-01 14:37 +1000
            Re: in need of some help... Alex Norton <ayjayn1101@gmail.com> - 2013-05-01 03:27 -0700
          Re: in need of some help... jt@toerring.de (Jens Thoms Toerring) - 2013-05-01 12:15 +0000
            Re: in need of some help... Alex Norton <ayjayn1101@gmail.com> - 2013-05-01 16:24 -0700
            Re: in need of some help... Alex Norton <ayjayn1101@gmail.com> - 2013-05-10 01:23 -0700
              Re: in need of some help... jt@toerring.de (Jens Thoms Toerring) - 2013-05-11 22:20 +0000
                Re: in need of some help... Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-05-12 17:59 +0200
                Re: in need of some help... jt@toerring.de (Jens Thoms Toerring) - 2013-05-12 21:56 +0000
                Re: in need of some help... Chris Angelico <rosuav@gmail.com> - 2013-05-13 08:18 +1000
                Re: in need of some help... jt@toerring.de (Jens Thoms Toerring) - 2013-05-12 22:58 +0000
                Re: in need of some help... Chris Angelico <rosuav@gmail.com> - 2013-05-13 09:08 +1000
                Re: in need of some help... Chris Angelico <rosuav@gmail.com> - 2013-05-13 10:51 +1000
                Re: in need of some help... Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-12 19:47 -0400
                Re: in need of some help... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-05-13 20:10 +1200
                Re: in need of some help... Chris Angelico <rosuav@gmail.com> - 2013-05-13 02:08 +1000
                Re: in need of some help... Alex Norton <ayjayn1101@gmail.com> - 2013-05-12 12:34 -0700

csiph-web