Merge pull request #2523 from PySimpleGUI/Dev-latest

Dev latest
This commit is contained in:
PySimpleGUI 2020-01-18 08:21:23 -05:00 committed by GitHub
commit 2f8494ce4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 10 deletions

View File

@ -37,7 +37,7 @@ class Playfield():
self.add_wall((0, 400), (600, 400)) # ground self.add_wall((0, 400), (600, 400)) # ground
self.add_wall((0, 0), (0, 600)) # Left side self.add_wall((0, 0), (0, 600)) # Left side
self.add_wall((600, 0), (600, 400)) # right side self.add_wall((600, 0), (600, 400)) # right side
self.arena_balls = [] # type: [] Ball self.arena_balls = [] # type: List[Ball]
self.graph_elem = graph_elem # type: sg.Graph self.graph_elem = graph_elem # type: sg.Graph
def add_wall(self, pt_from, pt_to): def add_wall(self, pt_from, pt_to):
@ -45,6 +45,7 @@ class Playfield():
ground_shape = pymunk.Segment(body, pt_from, pt_to, 0.0) ground_shape = pymunk.Segment(body, pt_from, pt_to, 0.0)
ground_shape.friction = 0.8 ground_shape.friction = 0.8
ground_shape.elasticity = .99 ground_shape.elasticity = .99
ground_shape.mass = pymunk.inf
self.space.add(ground_shape) self.space.add(ground_shape)
def add_random_balls(self): def add_random_balls(self):
@ -82,9 +83,11 @@ layout = [[sg.Text('Ball Test'), sg.Text('My IP {}'.format(hostname))],
sg.Button('Player 2 Shoot', size=(15, 2)), sg.Button('Exit')] sg.Button('Player 2 Shoot', size=(15, 2)), sg.Button('Exit')]
] ]
window = sg.Window('Window Title', layout, disable_close=True) window = sg.Window('Window Title', layout, disable_close=True, finalize=True)
area = Playfield(graph_elem) area = Playfield(graph_elem)
area.add_wall((0,300), (300,300))
graph_elem.draw_line((0,300),(300,300))
# area.add_random_balls() # area.add_random_balls()
# ------------------- GUI Event Loop ------------------- # ------------------- GUI Event Loop -------------------

View File

@ -25,7 +25,8 @@ WIN_MARGIN = 60
WIN_COLOR = "#282828" WIN_COLOR = "#282828"
TEXT_COLOR = "#ffffff" TEXT_COLOR = "#ffffff"
DEFAULT_DISPLAY_DURATION_IN_MILLISECONDS = 3000 DEFAULT_DISPLAY_DURATION_IN_MILLISECONDS = 3000 # how long to display the window
DEFAULT_FADE_IN_DURATION = 2000 # how long to fade in / fade out the window
# Base64 Images to use as icons in the window # Base64 Images to use as icons in the window
image64_error = b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAADlAAAA5QGP5Zs8AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAIpQTFRF////20lt30Bg30pg4FJc409g4FBe4E9f4U9f4U9g4U9f4E9g31Bf4E9f4E9f4E9f4E9f4E9f4FFh4Vdm4lhn42Bv5GNx5W575nJ/6HqH6HyI6YCM6YGM6YGN6oaR8Kev9MPI9cbM9snO9s3R+Nfb+dzg+d/i++vt/O7v/fb3/vj5//z8//7+////KofnuQAAABF0Uk5TAAcIGBktSYSXmMHI2uPy8/XVqDFbAAAA8UlEQVQ4y4VT15LCMBBTQkgPYem9d9D//x4P2I7vILN68kj2WtsAhyDO8rKuyzyLA3wjSnvi0Eujf3KY9OUP+kno651CvlB0Gr1byQ9UXff+py5SmRhhIS0oPj4SaUUCAJHxP9+tLb/ezU0uEYDUsCc+l5/T8smTIVMgsPXZkvepiMj0Tm5txQLENu7gSF7HIuMreRxYNkbmHI0u5Hk4PJOXkSMz5I3nyY08HMjbpOFylF5WswdJPmYeVaL28968yNfGZ2r9gvqFalJNUy2UWmq1Wa7di/3Kxl3tF1671YHRR04dWn3s9cXRV09f3vb1fwPD7z9j1WgeRgAAAABJRU5ErkJggg==' image64_error = b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAADlAAAA5QGP5Zs8AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAIpQTFRF////20lt30Bg30pg4FJc409g4FBe4E9f4U9f4U9g4U9f4E9g31Bf4E9f4E9f4E9f4E9f4E9f4FFh4Vdm4lhn42Bv5GNx5W575nJ/6HqH6HyI6YCM6YGM6YGN6oaR8Kev9MPI9cbM9snO9s3R+Nfb+dzg+d/i++vt/O7v/fb3/vj5//z8//7+////KofnuQAAABF0Uk5TAAcIGBktSYSXmMHI2uPy8/XVqDFbAAAA8UlEQVQ4y4VT15LCMBBTQkgPYem9d9D//x4P2I7vILN68kj2WtsAhyDO8rKuyzyLA3wjSnvi0Eujf3KY9OUP+kno651CvlB0Gr1byQ9UXff+py5SmRhhIS0oPj4SaUUCAJHxP9+tLb/ezU0uEYDUsCc+l5/T8smTIVMgsPXZkvepiMj0Tm5txQLENu7gSF7HIuMreRxYNkbmHI0u5Hk4PJOXkSMz5I3nyY08HMjbpOFylF5WswdJPmYeVaL28968yNfGZ2r9gvqFalJNUy2UWmq1Wa7di/3Kxl3tF1671YHRR04dWn3s9cXRV09f3vb1fwPD7z9j1WgeRgAAAABJRU5ErkJggg=='
@ -34,7 +35,21 @@ image64_success = b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAA3NCSVQICAjb
# ------------------------------------------------------------------- # -------------------------------------------------------------------
def _display_notification(title, message, icon=image64_success, display_duration_in_ms=DEFAULT_DISPLAY_DURATION_IN_MILLISECONDS, use_fade_in=True, alpha=0.9, location=None): def _display_notification(title, message, icon=image64_success, display_duration_in_ms=DEFAULT_DISPLAY_DURATION_IN_MILLISECONDS, fade_in_duration=DEFAULT_FADE_IN_DURATION, alpha=0.9, location=None):
"""
Displays a "notification window", usually in the bottom right corner of your display. Has an icon, a title, and a message
The window will slowly fade in and out if desired. Clicking on the window will cause it to move through the end the current "phase". For
example, if the window was fading in and it was clicked, then it would immediately stop fading in and instead be fully visible. It
a way for the user to quickly dismiss the window.
:param title: (str) Text to be shown at the top of the window in a larger font
:param message: (str) Text message that makes up the majority of the window
:param icon: (base64) A base64 encoded PNG/GIF image that will be displayed in the window
:param display_duration_in_ms: (int) Number of milliseconds to show the window
:param fade_in_duration: (int) Number of milliseconds to fade window in and out
:param alpha: (float) Alpha channel. 0 - invisible 1 - fully visible
:param location: Tuple[int, int] Location on the screen to display the window
:return: (Any) The Process ID returned from calling multiprocessing.Process
"""
# Compute location and size of the window # Compute location and size of the window
message = textwrap.fill(message, 50) message = textwrap.fill(message, 50)
win_msg_lines = message.count("\n") + 1 win_msg_lines = message.count("\n") + 1
@ -55,10 +70,10 @@ def _display_notification(title, message, icon=image64_success, display_duration
window["-GRAPH-"].draw_text(message, location=(64, 44), color=TEXT_COLOR, font=("Arial", 9), text_location=sg.TEXT_LOCATION_TOP_LEFT) window["-GRAPH-"].draw_text(message, location=(64, 44), color=TEXT_COLOR, font=("Arial", 9), text_location=sg.TEXT_LOCATION_TOP_LEFT)
window["-GRAPH-"].set_cursor('hand2') window["-GRAPH-"].set_cursor('hand2')
if use_fade_in == True: if fade_in_duration:
for i in range(1,int(alpha*100)): # fade in for i in range(1,int(alpha*100)): # fade in
window.set_alpha(i/100) window.set_alpha(i/100)
event, values = window.read(timeout=20) event, values = window.read(timeout=fade_in_duration // 100)
if event != sg.TIMEOUT_KEY: if event != sg.TIMEOUT_KEY:
window.set_alpha(1) window.set_alpha(1)
break break
@ -66,7 +81,7 @@ def _display_notification(title, message, icon=image64_success, display_duration
if event == sg.TIMEOUT_KEY: if event == sg.TIMEOUT_KEY:
for i in range(int(alpha*100),1,-1): # fade out for i in range(int(alpha*100),1,-1): # fade out
window.set_alpha(i/100) window.set_alpha(i/100)
event, values = window.read(timeout=20) event, values = window.read(timeout=fade_in_duration // 100)
if event != sg.TIMEOUT_KEY: if event != sg.TIMEOUT_KEY:
break break
else: else:
@ -76,14 +91,28 @@ def _display_notification(title, message, icon=image64_success, display_duration
window.close() window.close()
def display_notification(title, message, icon=image64_success, display_duration_in_ms=DEFAULT_DISPLAY_DURATION_IN_MILLISECONDS, use_fade_in=True, alpha=0.9, location=None): def display_notification(title, message, icon=image64_success, display_duration_in_ms=DEFAULT_DISPLAY_DURATION_IN_MILLISECONDS, fade_in_duration=DEFAULT_FADE_IN_DURATION, alpha=0.9, location=None):
proc = Process(target=_display_notification, args=(title, message, icon,display_duration_in_ms, use_fade_in, alpha, location)) """
Displays a "notification window", usually in the bottom right corner of your display. Has an icon, a title, and a message
The window will slowly fade in and out if desired. Clicking on the window will cause it to move through the end the current "phase". For
example, if the window was fading in and it was clicked, then it would immediately stop fading in and instead be fully visible. It
a way for the user to quickly dismiss the window.
:param title: (str) Text to be shown at the top of the window in a larger font
:param message: (str) Text message that makes up the majority of the window
:param icon: (base64) A base64 encoded PNG/GIF image that will be displayed in the window
:param display_duration_in_ms: (int) Number of milliseconds to show the window
:param fade_in_duration: (int) Number of milliseconds to fade window in and out
:param alpha: (float) Alpha channel. 0 - invisible 1 - fully visible
:param location: Tuple[int, int] Location on the screen to display the window
:return: (Any) The Process ID returned from calling multiprocessing.Process
"""
proc = Process(target=_display_notification, args=(title, message, icon, display_duration_in_ms, fade_in_duration, alpha, location))
proc.start() proc.start()
return proc return proc
if __name__ == '__main__': if __name__ == '__main__':
proc2 = display_notification('Normal Location', 'This is my notification!') proc2 = display_notification('Normal Location', 'This is my notification!')
proc3 = display_notification('Upper Left', 'This one does not fade in!', icon=image64_error, location=(0,0), use_fade_in=False) proc3 = display_notification('Upper Left', 'This one does not fade in!', icon=image64_error, location=(0,0), fade_in_duration=0)
proc3.join() proc3.join()
proc2.join() proc2.join()