10 examples of 'python get screen size' in Python

Every line of 'python get screen size' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
15def get_screen_size():
16 return [int(x) for x in os.popen("stty size", "r").read().split()]
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

144def get_screen_wh(self):
145 return self.c.screen_size()
188def _height_and_width(self):
189 """
190 Query console for dimensions
191 Returns named tuple (columns, lines)
192 """
193
194 size = None
195
196 # In Python 3.3+ we can let the standard library handle this
197 if GTS_SUPPORTED:
198 try:
199 size = os.get_terminal_size(self.stream_fd)
200 except (ValueError, OSError):
201 pass
202
203 else:
204 try:
205 window = get_csbi(self.stream_fh).srWindow
206 size = TerminalSize(window.Right - window.Left + 1, window.Bottom - window.Top + 1)
207 except OSError:
208 pass
209
210 if size is None:
211 size = TerminalSize(int(os.getenv('COLUMNS', '80')), int(os.getenv('LINES', '25')))
212
213 return size
272def _get_terminal_size_windows():
273 res=None
274 try:
275 from ctypes import windll, create_string_buffer
276
277 # stdin handle is -10
278 # stdout handle is -11
279 # stderr handle is -12
280
281 h = windll.kernel32.GetStdHandle(-12)
282 csbi = create_string_buffer(22)
283 res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
284 except:
285 return None
286 if res:
287 import struct
288 (bufx, bufy, curx, cury, wattr,
289 left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
290 sizex = right - left + 1
291 sizey = bottom - top + 1
292 return sizex, sizey
293 else:
294 return None
69def _get_terminal_size_windows():
70
71 try:
72 from ctypes import windll, create_string_buffer
73
74 # stdin handle is -10
75 # stdout handle is -11
76 # stderr handle is -12
77
78 h = windll.kernel32.GetStdHandle(-12)
79 csbi = create_string_buffer(22)
80 res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
81 except (AttributeError, ValueError):
82 return None
83 if res:
84 import struct
85 (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx,
86 maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
87 sizex = right - left + 1
88 sizey = bottom - top + 1
89 return sizex, sizey
90 else:
91 return None
17def get_scr_size():
18 return [int(x) for x in os.popen("stty size", "r").read().split()]
279def calc_size(self):
280 """put it at the bottom of the screen"""
281 self.height = HEIGHT_CON
282 self.width = self.termwidth - int(self.termwidth / 2) - 2
283 self.posy = self.termheight - self.height
101def get_terminal_size():
102 """Return the size in columns, rows for terminal window
103
104 This method will attempt to determine the current terminal window size.
105 If it cannot, it returns the default of (80, 25) = 80 characters
106 on a line and 25 lines.
107
108 Returns tuple - (x, y) = max colum (# chars), max rows
109 """
110 default = (80, 25)
111 try:
112 if os.name == "posix":
113 import fcntl
114 import termios
115 y, x = 0, 1
116 packed_info = fcntl.ioctl(0, termios.TIOCGWINSZ,
117 struct.pack('HHHH', 0, 0, 0, 0))
118 wininfo = struct.unpack('HHHH', packed_info)
119 return (wininfo[x], wininfo[y])
120 else:
121 from ctypes import windll, create_string_buffer
122
123 # -11 == stdout
124 handle = windll.kernel32.GetStdHandle(-11)
125 strbuff = create_string_buffer(22)
126 windll.kernel32.GetConsoleScreenBufferInfo(handle, strbuff)
127 left, top, right, bottom = 5, 6, 7, 8
128 wininfo = struct.unpack("hhhhHhhhhhh", strbuff)
129 x = wininfo[right] - wininfo[left] + 1
130 y = wininfo[bottom] - wininfo[top] + 1
131 return (x, y)
132 except:
133 pass # silence! just return default on error.
134 return default
86def set_canvas_size(
87 self, canvas_x=None, canvas_y=None,
88 resize_window=True, fullscreen=False
89 ):
90 """Change the logical canvas size and determine window/display sizes."""
91 if canvas_x is not None and canvas_y is not None:
92 self._canvas_logical = canvas_x, canvas_y
93 self._calculate_border_shift()
94 old_display_size = self._display_size
95 old_window_size = self._window_size
96 if resize_window or self._force_native_pixel:
97 # comply with requested size
98 if self._force_window_size:
99 self._window_size = self._force_window_size
100 else:
101 slack_ratio = _SLACK_RATIO if not fullscreen else 1.
102 if not self._force_native_pixel:
103 self._window_size = self._find_nonnative_window_size(slack_ratio)
104 else:
105 self._window_size = self._find_native_window_size(slack_ratio)
106 if fullscreen:
107 self._display_size = self._screen_size
108 else:
109 self._display_size = self._window_size
110 self._calculate_scale()
111 self._calculate_letterbox_shift()
112 return self._display_size != old_display_size or self._window_size != old_window_size
86def _get_term_size_windows():
87 """Discover the size of the user's terminal on Microsoft Windows.
88
89 Returns:
90 tuple: (width, height) tuple giving the dimensions of the user's terminal window in characters,
91 or None if the size could not be determined.
92 """
93 res = None
94 try:
95 from ctypes import windll, create_string_buffer
96 # stdin handle is -10, stdout -11, stderr -12
97 handle = windll.kernel32.GetStdHandle(-12)
98 csbi = create_string_buffer(22)
99 res = windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi)
100 except: # pylint: disable=bare-except
101 return None
102 if res:
103 import struct
104 (_, _, _, _, _, left, top, right, bottom, _, _) = struct.unpack("hhhhHhhhhhh", csbi.raw)
105 sizex = right - left + 1
106 sizey = bottom - top + 1
107 return sizex, sizey
108 return None

Related snippets