Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 20th, 2013, 01:59 PM
xtd xtd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 xtd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 7 sec
Reputation Power: 0
While with responses to keyboard

Hi all,

I'm a rather inexperienced python programmer. I'm looking for some construct that will start an indefinite loop (eg. while True or something similar) and the program will respond to the keyboard events. Something rather simple: q - get out of the loop and quit, 1 - do something, 2 - do something.

Code:
def some_function:
    pass

def other_function:
    pass


my loop:
    if key == "1":
        some_function
    else if key == "2":
        other_function


Thanking you all.

etc.

I think it is called event driven programming. I can't find anything relevant. It's all about Twisted which looks like has a lot of opponents. Could you recommend something? I don't need/want GUI and I'd like something different than "input" where you need to press enter after the user input.

Reply With Quote
  #2  
Old January 20th, 2013, 04:09 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,367 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 9 m 23 sec
Reputation Power: 383
curses
http://docs.python.org/3/library/curses.html
should help. You'll want raw or cbreak mode.

Remember the command
stty sane
or you could use the atexit module to register a cleanup function that restores normal terminal functionality.
Comments on this post
xtd agrees: Useful post
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old January 20th, 2013, 05:19 PM
xtd xtd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 xtd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 7 sec
Reputation Power: 0
Thank you. That's exactly what I wanted.

Reply With Quote
  #4  
Old January 20th, 2013, 05:58 PM
xtd xtd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 xtd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 7 sec
Reputation Power: 0
One more question. I'm trying to get the centre of a window so that I can position, eg. a title right in the middle regardless of the size of my terminal window.

Thank you

Reply With Quote
  #5  
Old January 20th, 2013, 08:33 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,367 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 9 m 23 sec
Reputation Power: 383
Code:
>>> import curses
>>> w = curses.initscr()
>>> w.getbegyx()
(0, 0)
>>> w.getmaxyx()
(24, 80)
>>> w.addstr(12,38,'hi!')
>>> w.refresh()

Reply With Quote
  #6  
Old January 21st, 2013, 10:22 AM
xtd xtd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 xtd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 7 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Code:
>>> import curses
>>> w = curses.initscr()
>>> w.getbegyx()
(0, 0)
>>> w.getmaxyx()
(24, 80)
>>> w.addstr(12,38,'hi!')
>>> w.refresh()


Excellent. Thank you.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > While with responses to keyboard

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap