-
Notifications
You must be signed in to change notification settings - Fork 10
/
win_size.py
executable file
·106 lines (81 loc) · 2.75 KB
/
win_size.py
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
#!/usr/bin/env python
# vi:ts=4 sw=4 et
import argparse
# Easy way of importing pygame:
# import pygame
# Hard way of importing pygame:
import pygame.display
import pygame.event
import pygame.key
from pygame.locals import *
def set_size(width, height):
screen = pygame.display.set_mode((width, height), RESIZABLE)
size_string = "{0}x{1}".format(width, height)
pygame.display.set_caption(size_string)
print(size_string)
yellow = (255, 255, 0)
screen.fill(yellow)
pygame.display.flip()
def change_size(delta_width, delta_height):
# pygame.display.get_surface().get_size()
info = pygame.display.Info()
w = info.current_w + delta_width
h = info.current_h + delta_height
set_size(w, h)
def parse_arguments():
parser = argparse.ArgumentParser(
description="Opens a window with the specified size.",
epilog="""
This script uses Pygame to open a window painted with yellow color. Use
arrow keys to increase/decrese the window dimensions. The current size
<width>x<height> is shown at the window title, and also printed to the
stdout. In order to understand why this script was written, read
https://web.archive.org/web/20111117170531/http://my.opera.com:80/CrazyTerabyte/blog/2011/10/13/nvidia-bug-when-rendering-windows-smaller-than-32x32""",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"width",
action="store",
type=int,
help="initial window width"
)
parser.add_argument(
"height",
action="store",
type=int,
help="initial window height"
)
args = parser.parse_args()
return args
def main():
options = parse_arguments()
# pygame.init() will try to initialize all pygame modules, including
# Cdrom and Audio. I'm not using such things, so let's initialize the
# only really required module:
pygame.display.init()
set_size(options.width, options.height)
# This does not work... :-(
pygame.key.set_repeat(500, 50)
key_and_offsets = {
# Key: (delta_width, delta_height)
K_UP: ( 0, -1),
K_DOWN: ( 0, +1),
K_LEFT: (-1, 0),
K_RIGHT: (+1, 0),
}
while True:
event = pygame.event.wait()
if event.type == QUIT:
break
elif event.type == KEYDOWN:
if event.key in [K_ESCAPE, K_q]:
break
elif event.key in key_and_offsets:
delta_width, delta_height = key_and_offsets.get(event.key)
change_size(delta_width, delta_height)
elif event.type == VIDEORESIZE:
w, h = event.size
set_size(w, h)
pygame.quit()
if __name__ == "__main__":
main()