Skip to content

Commit 8c38dd3

Browse files
authored
Merge pull request #447 from MyreMylar/fix-panel-container
improve handling of showing/hiding for more elements
2 parents 9eb3cb4 + bfc3748 commit 8c38dd3

File tree

7 files changed

+34
-23
lines changed

7 files changed

+34
-23
lines changed

pygame_gui/elements/ui_drop_down_menu.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,10 @@ def __init__(self,
666666
*,
667667
expand_on_option_click: bool = True
668668
):
669-
669+
# Need to move some declarations early as they are indirectly referenced via the ui element
670+
# constructor
671+
self.menu_states = None
672+
self.current_state = None
670673
super().__init__(relative_rect, manager, container=container,
671674
starting_height=0,
672675
anchors=anchors,
@@ -971,8 +974,8 @@ def show(self):
971974
showing it's buttons.
972975
"""
973976
super().show()
974-
975-
self.menu_states['closed'].show()
977+
if self.current_state is not None and self.menu_states is not None:
978+
self.menu_states['closed'].show()
976979

977980
def hide(self):
978981
"""
@@ -981,7 +984,7 @@ def hide(self):
981984
call the hide() method of the 'closed' state which hides all it's children widgets.
982985
"""
983986
super().hide()
984-
985-
if self.current_state == self.menu_states['expanded']:
986-
self.menu_states['expanded'].hide()
987-
self.menu_states['closed'].hide()
987+
if self.current_state is not None and self.menu_states is not None:
988+
if self.current_state == self.menu_states['expanded']:
989+
self.menu_states['expanded'].hide()
990+
self.menu_states['closed'].hide()

pygame_gui/elements/ui_horizontal_scroll_bar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,8 @@ def show(self):
578578
will propagate and show all the buttons.
579579
"""
580580
super().show()
581-
582-
self.button_container.show()
581+
if self.button_container is not None:
582+
self.button_container.show()
583583

584584
def hide(self):
585585
"""

pygame_gui/elements/ui_horizontal_slider.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __init__(self,
4444
visible: int = 1,
4545
click_increment: Union[float, int] = 1
4646
):
47-
47+
# Need to move some declarations early as they are indirectly referenced via the ui element
48+
# constructor
4849
self.sliding_button = None
4950
self.button_container = None
5051
super().__init__(relative_rect, manager, container,
@@ -560,8 +561,8 @@ def show(self):
560561
the button_container which will propagate and show the left and right buttons.
561562
"""
562563
super().show()
563-
564-
self.sliding_button.show()
564+
if self.sliding_button is not None:
565+
self.sliding_button.show()
565566
if self.button_container is not None:
566567
self.button_container.show()
567568

pygame_gui/elements/ui_panel.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def __init__(self,
5353
anchors: Optional[Dict[str, Union[str, UIElement]]] = None,
5454
visible: int = 1
5555
):
56-
56+
# Need to move some declarations early as they are indirectly referenced via the ui element
57+
# constructor
58+
self.panel_container = None
5759
super().__init__(relative_rect,
5860
manager,
5961
container,
@@ -300,12 +302,13 @@ def show(self):
300302
In addition to the base UIElement.show() - call show() of owned container - panel_container.
301303
"""
302304
super().show()
303-
304-
self.panel_container.show()
305+
if self.panel_container is not None:
306+
self.panel_container.show()
305307

306308
def hide(self):
307309
"""
308310
In addition to the base UIElement.hide() - call hide() of owned container - panel_container.
309311
"""
310-
self.panel_container.hide()
312+
if self.panel_container is not None:
313+
self.panel_container.hide()
311314
super().hide()

pygame_gui/elements/ui_scrolling_container.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def __init__(self,
4141
object_id: Optional[Union[ObjectID, str]] = None,
4242
anchors: Optional[Dict[str, Union[str, UIElement]]] = None,
4343
visible: int = 1):
44-
44+
# Need to move some declarations early as they are indirectly referenced via the ui element
45+
# constructor
46+
self._root_container = None
4547
super().__init__(relative_rect,
4648
manager,
4749
container,
@@ -439,7 +441,8 @@ def show(self):
439441
separately.
440442
"""
441443
super().show()
442-
self._root_container.show()
444+
if self._root_container is not None:
445+
self._root_container.show()
443446

444447
def hide(self):
445448
"""
@@ -448,5 +451,6 @@ def hide(self):
448451
it's visibility will propagate to them - there is no need to call their hide() methods
449452
separately.
450453
"""
451-
self._root_container.hide()
454+
if self._root_container is not None:
455+
self._root_container.hide()
452456
super().hide()

pygame_gui/elements/ui_selection_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@ def show(self):
710710
there is no need to call their show() methods separately.
711711
"""
712712
super().show()
713-
714-
self.list_and_scroll_bar_container.show()
713+
if self.list_and_scroll_bar_container is not None:
714+
self.list_and_scroll_bar_container.show()
715715

716716
def hide(self):
717717
"""

pygame_gui/elements/ui_vertical_scroll_bar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ def show(self):
567567
propagate and show all the buttons.
568568
"""
569569
super().show()
570-
571-
self.button_container.show()
570+
if self.button_container is not None:
571+
self.button_container.show()
572572

573573
def hide(self):
574574
"""

0 commit comments

Comments
 (0)