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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Randall Sturgis
    New Member
    • Jan 2011
    • 1

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

    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.
    Last edited by Randall Sturgis; Jan 1 '11, 04:31 AM. Reason: additional info
  • Mariostg
    Contributor
    • Sep 2010
    • 332

    #2
    Did you look at python curses

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      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.
      Code:
      from Tkinter import *
      
      class MenuTest():
          def __init__(self, top):
              ##---  always place in upper left corner ( +10+10 )
              ##     size = 150x150 = minimum size so it's big enough to be seen
              top.geometry("150x150+10+10" )
      
              label1 = Label( top, text = "Test Menu" )
              label1.pack()
      
              label2 = Label( top, text = "" )          ## blank line = spacer
              label2.pack()
      
              self.button1_lit = StringVar()
              self.button1_lit.set("Option 1")
              option1 = Button(top, textvariable=self.button1_lit,
                        command=self.callback1, bg='blue', fg='white' )
              option1.pack(fill=X, expand=1)
      
              option2 = Button(top, text='Option 2',
                        command=self.callback2, bg='green', fg='white' )
              option2.pack(fill=X, expand=1)
      
              option3 = Button(top, text='Option 3 - No Exit',
                        command=self.callback3, bg='black', fg='white' )
              option3.pack(fill=X, expand=1)
      
              exit=  Button(top, text='EXIT',
                        command=top.quit, bg='red', fg='white' )
              exit.pack(fill=X, expand=1)
      
      
          def callback1(self) :
              print "Callback #1"
              self.button1_lit.set("SELECTED")
      
          def callback2(self) :
              self.button1_lit.set("Button 1")
              print "Callback #2"
      
          def callback3(self) :
              self.button1_lit.set("Button 1")
              print "Callback #3"
      
      ##===============================================================
      top = Tk()
      MT=MenuTest(top)
      
      top.mainloop()
      Also consider using radio buttons. This example is from effbot's site http://effbot.org/tkinterbook/radiobutton.htm
      Code:
      from Tkinter import *
      
      master = Tk()
      master.geometry("100x50")
      
      v = IntVar()
      
      Radiobutton(master, text="One", variable=v, value=1).pack(anchor=W)
      Radiobutton(master, text="Two", variable=v, value=2).pack(anchor=W)
      
      mainloop()

      Comment

      Working...