By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

How can I write (put) a character in a specific spot on a dos console?

P: 1
I am coding a fun little text base game to get back into programming. Most of the code is in python but I'm only designing it for a dos console so I'm occasionally using dos commands via Pythons "os" module.

I want to be able to put a character to the console without "redrawing" the current screen line by line. I hoped there would be a command like "print []" but where it would not just create a new line, but instead let me specify exactly where on the screen I want to write a character or a string.

Scenario:
I print 3 options for the user to select. The first option has "*" by it to show that it is selected. If the user hits the down arrow key, the second option should now be selected. Instead of clearing the whole screen and re-printing all three options, but with the "*" now by the second option, I want to just "erase" the "*" by the first option, and "move" it to the second one, leaving everything else on the screen as it was.
Jan 1 '11 #1
Share this Question
Share on Google+
2 Replies


100+
P: 332
Did you look at python curses
Jan 12 '11 #2

Expert 100+
P: 624
A canvas widget, which is in every GUI is probably a good option here. Something kind of similar is a menu which you can look at and adapt. Note that button #1 is the only one that changes when any button is pressed.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class MenuTest():
  4.     def __init__(self, top):
  5.         ##---  always place in upper left corner ( +10+10 )
  6.         ##     size = 150x150 = minimum size so it's big enough to be seen
  7.         top.geometry("150x150+10+10" )
  8.  
  9.         label1 = Label( top, text = "Test Menu" )
  10.         label1.pack()
  11.  
  12.         label2 = Label( top, text = "" )          ## blank line = spacer
  13.         label2.pack()
  14.  
  15.         self.button1_lit = StringVar()
  16.         self.button1_lit.set("Option 1")
  17.         option1 = Button(top, textvariable=self.button1_lit,
  18.                   command=self.callback1, bg='blue', fg='white' )
  19.         option1.pack(fill=X, expand=1)
  20.  
  21.         option2 = Button(top, text='Option 2',
  22.                   command=self.callback2, bg='green', fg='white' )
  23.         option2.pack(fill=X, expand=1)
  24.  
  25.         option3 = Button(top, text='Option 3 - No Exit',
  26.                   command=self.callback3, bg='black', fg='white' )
  27.         option3.pack(fill=X, expand=1)
  28.  
  29.         exit=  Button(top, text='EXIT',
  30.                   command=top.quit, bg='red', fg='white' )
  31.         exit.pack(fill=X, expand=1)
  32.  
  33.  
  34.     def callback1(self) :
  35.         print "Callback #1"
  36.         self.button1_lit.set("SELECTED")
  37.  
  38.     def callback2(self) :
  39.         self.button1_lit.set("Button 1")
  40.         print "Callback #2"
  41.  
  42.     def callback3(self) :
  43.         self.button1_lit.set("Button 1")
  44.         print "Callback #3"
  45.  
  46. ##===============================================================
  47. top = Tk()
  48. MT=MenuTest(top)
  49.  
  50. top.mainloop() 
Also consider using radio buttons. This example is from effbot's site http://effbot.org/tkinterbook/radiobutton.htm
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. master = Tk()
  4. master.geometry("100x50")
  5.  
  6. v = IntVar()
  7.  
  8. Radiobutton(master, text="One", variable=v, value=1).pack(anchor=W)
  9. Radiobutton(master, text="Two", variable=v, value=2).pack(anchor=W)
  10.  
  11. mainloop() 
Jan 12 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.