I am making a text adventure and would like the command line window to be a specific size when you run the .py file in the command line window. eg. 300 wide by 250 height. I found a thread on here that shows how to specify the text colour which I also wanted to know. http://www.daniweb.com/forums/post1150939.html#post1150939 Can anybody help? Thanks.

Recommended Answers

All 11 Replies

My advice, run it from IDLE and you get an adjustable output window with a nice background that works across several common platforms.

Thanks for the advice but I'm trying to make my game retro, like the old text based games such as Zork etc. The green text on a black background is classic.

Thanks for the advice but I'm trying to make my game retro, like the old text based games such as Zork etc. The green text on a black background is classic.

Honestly, get pygame. Its easy to do a text based game with pygame. It will make a screen, and will update with text. Simple simple things.

Make a surface. Fill surface with black.
Make text surface. Write that text to the original surface.
Call for update, and repeat.

Thanks, but I havnt got into the pygame stuff yet.

Well that was just a suggestion. Good luck though

for the width try

import os
os.system("mode con cols=50")

and for the length

os.system("mode con lines=20")

#you could also combine them
os.system("mode con cols=50 lines=20")

Play around with the numbers until ya find a size you like.
This is windows only.

commented: This person knows their stuff! +1

THANKS ALOT DUDE! Finally I have the answer! Nice one!

Your welcome. and don't forget to mark as solved. :)

@Tech B

Is there a way of using variables to set the cols and lines values?

@DubWine hi, it seems that there are solutions by using the ctypes module and the setconsolewindoinfo method in windows api. See https://stackoverflow.com/questions/3646362/how-to-control-the-size-of-the-windows-shell-window-from-within-a-python-script for example. Also google the keywords python and setconsolewindowinfo.

Edit: you can also read the console dimension without a subprocess as in http://rosettacode.org/wiki/Terminal_control/Dimensions#Python

commented: + for Rosettacode. Amazing resource. +12

Can't seem to make an edit or comment to @Gribouillis, did I miss something?

Anyway... I figured it out; os.system() takes a string value so you can simply do:

width = "100"
height = "30"

os.system("mode con cols="+width+"lines="+height)

Thanks for the links @Gribouillis; ctypes looks more in depth and customisable so will check it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.