Welcome, guest | Sign In | My Account | Store | Cart

This code shows how to open up a default Terminal running Python to tha maximum allowed on the _desktop_. I decided on writing code to do a crude emulation of a Campimeter. Anyone who has worn spectacles for a long time has had this test done at the optician's. Originally written for a Macbook Pro, OSX 10.7.5 but also tested on Debian Linux 6.0.x. READ the code for more information!!! This code WILL alter the size of the Terminal window AND changes the colours too, although it is very easy to return back to the default state it is NOT included in the code... I EXPECT professionals to know how to doo that.

Before running Python inside the Terminal ensure the Terminal window is at the uppermost left hand corner of your desktop...

I needed the maximised Python Terminal for something else but this was the easiest way to demonstrate ho to do it through Python...

Written so that kids can understand what is going on...

Enjoy finding simple solutions to often very difficult problems... ;o)

Bazza, G0LCU...

Python, 138 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Campimeter.py
#
# I needed the facility to have a maximised Terminal window on OSX 10.7.5 and this was the result...
# A DEMO to show how to increase the size of a Terminal window and obtain the maximum usable size on a _desktop_.
# The Terminal window must be initially set manually at the uppermost left hand corner and Python started inside
# this window. The colours for this DEMO inside OSX 10.7.5, are grey background, red square and yellow asterisk/characters...
#
# Written in such a way as anyone can understand how it works, youngsters too.
#
# To run type from the Python prompt:-
#
# >>> exec(open("/full/path/to/Campimeter.py).read())<CR>
#
# Where "/full/path/to/" is where you have saved this code; then have a little fun...
#
# This code generates a crude Campimeter... http://en.wikipedia.org/wiki/Campimeter ...and is used to check
# visual field. Just focus on the red square in the centre of the window after pressing <CR> from the initial
# window and then press Ctrl-C immediately after you see an asterisk flash. It is possible to get the Ctrl-C to
# _crash_ out and stop the DEMO if Ctrl-C is pressed at any other time than just after seeing the asterisk flash.
# The cursor has not been turned off and will be seen on the left hand side of the window, so try to ignore it.
#
# $VER: Campimeter.py_Version_0.00.10_(C)2012_B.Walker_G0LCU.
# This code is issued as Public Domain and you may do with it as you please.
#
# I have not reset the Terminal back to its original state although all of the information how to do it is in
# this code. Colours are easily reset in Python and I have shown this many times on this site...
#
# Designed and tested on a Macbook Pro, OSX 10.7.5 using Python Versions 2.6.7 and 2.7.1. Also tested on Debian
# Linux 6.0.x using Python versions 2.6.6, 2.7.2 and 3.1.3...
# It would be interesting to see which other platform variants this idea works on; just add a comment...

# All imports required.
import os
import sys
import random
import time

# Ensure Python Version 3.x.x is included...
if sys.version[0]=="3": raw_input=input

# Set some values as global, my choice! ;o)
global row
global column
global attempts

# Allocate initial Terminal window values to be much greater than the allowed Terminal size on the desktop...
row=200
column=300
attempts=0

# Set the Terminal window size larger than its default!
sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=row,cols=column))

# Set up a basic user window starting with a grey, cleared window...
print("\033[1;93;100m")
n=os.system("clear")
# Add a delay to allow settings to settle...
time.sleep(1)
print("\n$VER: Campimeter.py_Version_0.00.10_(C)2012_B.Walker_G0LCU.\n")
print("Press the <CR> key to start. After this window is exited focus on")
print("the centred, coloured square. Position yourself until you are both")
print("comfortable and the screen presents a wide field of view. Use the two")
print("keys Ctrl-C if and when you see a randomly positioned _*_ displayed.\n")
print("The larger the display used the better, have fun...\n")
# time.sleep(1)
raw_input("Press the <CR> key to continue...")

# Now get the _new_ real size of the Terminal window...
rows,columns=os.popen("stty size","r").read().split()
row=int(rows)
column=int(columns)

def locate(user_string="$VER: Campimeter.py_Version_0.00.10_(C)2012-2013_B.Walker_G0LCU.",x=0,y=0):
	# Don't allow any user errors. Python's own error detection will check for
	# syntax and concatination, etc, etc, errors.
	x=int(x)
	y=int(y)
	if x>=255: x=255
	if y>=255: y=255
	if x<=0: x=0
	if y<=0: y=0
	HORIZ=str(x)
	VERT=str(y)
	# Plot the user_string at the starting position HORIZ, VERT...
	print("\033["+VERT+";"+HORIZ+"f"+user_string)

def main():
	# Ensure certain _variables_ remain global, my choice... ;o)
	global row
	global column
	global attempts

	# Note:- row and colour are already set.
	attempts=0

	# Clear the window for the test.
	n=os.system("clear")

	# Set the centred red square for the start...
	locate("\033[0;41;41m  \033[1;93;100m ",int(column/2),int(row/2))

	# This is set for 5 possible attempts only. Just change the number 5 to your number of attempts.
	for n in range(0,5,1):
		try:
			# Have a random start waiting time...
			waiting_time=int(random.random()*6)
			time.sleep(waiting_time)
			coloured_spot_column=int(random.random()*int(columns))
			coloured_spot_row=int(random.random()*int(rows))
			# Display a coloured asterisk randomly on screen for a brief period...
			locate("\033[1;93;100m*\033[1;93;100m ",coloured_spot_column,coloured_spot_row)
			# This is the timer to display the asterisk and is made relatively easy...
			time.sleep(0.2)
			# Clear this displayed asterisk...
			locate("\033[1;93;100m \033[1;93;100m ",coloured_spot_column,coloured_spot_row)
			# Because this code does not detect whether the red square might be overwritten,
			# ensure that it is redrawn at the centre of the Terminal window.
			locate("\033[0;41;41m  \033[1;93;100m ",int(column/2),int(row/2))
			# Hold long enough to react to Ctrl-C...
			time.sleep(1)
		except KeyboardInterrupt:
			# After pressing Ctrl-C ensure ^c or ^C is not seen on the screen and update.
			print("\b\b\b\b    ")
			attempts=attempts+1
			time.sleep(1)

	# Give the results for this fun project and re-run if required...
	print("You had "+str(attempts)+" successes out of "+str((n+1))+" possible attempts...\n")
	again=raw_input("Do you want to try again? (Y/N):- ")
	if again=="y" or again=="Y": main()

main()
# It is easy to reset the Terminal back to the default sze AND colours using the methods already shown.
# I have not bothered, as the intent of this DEMO is to show how to increase the size of the Terminal
# window and obtain the real, new size too.

# End of Campimeter.py DEMO.
# Enjoy finding simple solutuions to often very difficult problems...

I would greatly appreciate any testing done on other platforms as to whether this works or not for the said platforms... Please reply in the comments, many thanks...

NOTE:- This will NOT work on windows!!!

Bazza, G0LCU...

2 comments

Barry Walker (author) 11 years, 4 months ago  # | flag

Whoever it was that voted this up, many thanks from me...

I am a mere amateur coder and can only code in my own time...

Again many thanks...

Bazza, G0LCU...

Barry Walker (author) 7 years, 10 months ago  # | flag

To the person that voted this up another notch on an unknown date to me...

Many thanks.