Skip to content

Commit 9eb123a

Browse files
committed
Used a single application instance in examples.
Each program must create only a single instance of `tkinter.Tk`, `wx.App`, `PyQt5.QtWidgets.QApplication` or `PyQt6.QtWidgets.QApplication`.
1 parent 28347d1 commit 9eb123a

File tree

4 files changed

+117
-135
lines changed

4 files changed

+117
-135
lines changed

examples/examples_ScrollableAreaQt5.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,47 @@
66

77
from ScrollableContainers.Qt5 import ScrollableAreaQt5
88

9-
###############################################################################
109

11-
def grid_of_widgets():
12-
app = QApplication([])
13-
window = QMainWindow()
14-
window.setWindowTitle('`ScrollableAreaQt5` demo')
10+
class ExamplesScrollableAreaQt5:
1511

16-
# Create a scrollable area.
17-
scrollable_area = ScrollableAreaQt5()
12+
def __init__(self):
13+
self.grid_of_widgets(QMainWindow())
14+
self.single_widget(QMainWindow())
1815

19-
# Add widgets to the `area` attribute of the scrollable area, not to the
20-
# scrollable area itself.
21-
dim = 10
22-
grid_layout = QGridLayout(scrollable_area.area)
23-
for (i, j) in itertools.product(range(dim), repeat=2):
24-
grid_layout.addWidget(QLabel(text=f'Label\n({i}, {j})'), i, j)
16+
def grid_of_widgets(self, window):
17+
self.win1 = window
18+
window.setWindowTitle('`ScrollableAreaQt5` demo')
2519

26-
window.setCentralWidget(scrollable_area)
27-
window.show()
28-
app.exec()
20+
# Create a scrollable area.
21+
scrollable_area = ScrollableAreaQt5()
2922

30-
###############################################################################
23+
# Add widgets to the `area` attribute of the scrollable area, not to
24+
# the scrollable area itself.
25+
dim = 10
26+
grid_layout = QGridLayout(scrollable_area.area)
27+
for (i, j) in itertools.product(range(dim), repeat=2):
28+
label = QLabel(text=f'Label\n({i}, {j})')
29+
grid_layout.addWidget(label, i, j)
3130

32-
def single_widget():
33-
app = QApplication([])
34-
window = QMainWindow(size=QSize(600, 200))
35-
window.setWindowTitle('`ScrollableAreaQt5` demo')
31+
window.setCentralWidget(scrollable_area)
32+
window.show()
3633

37-
scrollable_area = ScrollableAreaQt5()
34+
def single_widget(self, window):
35+
self.win2 = window
36+
window.setWindowTitle('`ScrollableAreaQt5` demo')
37+
window.resize(QSize(600, 200))
3838

39-
vbox = QVBoxLayout(scrollable_area.area)
40-
vbox.addWidget(QLabel(text='big window, small label'))
39+
scrollable_area = ScrollableAreaQt5()
4140

42-
window.setCentralWidget(scrollable_area)
43-
window.show()
44-
app.exec()
41+
vbox = QVBoxLayout(scrollable_area.area)
42+
label = QLabel(text='big window, small label')
43+
vbox.addWidget(label)
44+
45+
window.setCentralWidget(scrollable_area)
46+
window.show()
4547

46-
###############################################################################
4748

4849
if __name__ == '__main__':
49-
grid_of_widgets()
50-
single_widget()
50+
app = QApplication([])
51+
examples = ExamplesScrollableAreaQt5()
52+
app.exec()

examples/examples_ScrollableAreaQt6.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,47 @@
66

77
from ScrollableContainers.Qt6 import ScrollableAreaQt6
88

9-
###############################################################################
109

11-
def grid_of_widgets():
12-
app = QApplication([])
13-
window = QMainWindow()
14-
window.setWindowTitle('`ScrollableAreaQt6` demo')
10+
class ExamplesScrollableAreaQt6:
1511

16-
# Create a scrollable area.
17-
scrollable_area = ScrollableAreaQt6()
12+
def __init__(self):
13+
self.grid_of_widgets(QMainWindow())
14+
self.single_widget(QMainWindow())
1815

19-
# Add widgets to the `area` attribute of the scrollable area, not to the
20-
# scrollable area itself.
21-
dim = 10
22-
grid_layout = QGridLayout(scrollable_area.area)
23-
for (i, j) in itertools.product(range(dim), repeat=2):
24-
grid_layout.addWidget(QLabel(text=f'Label\n({i}, {j})'), i, j)
16+
def grid_of_widgets(self, window):
17+
self.win1 = window
18+
window.setWindowTitle('`ScrollableAreaQt6` demo')
2519

26-
window.setCentralWidget(scrollable_area)
27-
window.show()
28-
app.exec()
20+
# Create a scrollable area.
21+
scrollable_area = ScrollableAreaQt6()
2922

30-
###############################################################################
23+
# Add widgets to the `area` attribute of the scrollable area, not to
24+
# the scrollable area itself.
25+
dim = 10
26+
grid_layout = QGridLayout(scrollable_area.area)
27+
for (i, j) in itertools.product(range(dim), repeat=2):
28+
label = QLabel(text=f'Label\n({i}, {j})')
29+
grid_layout.addWidget(label, i, j)
3130

32-
def single_widget():
33-
app = QApplication([])
34-
window = QMainWindow(size=QSize(600, 200))
35-
window.setWindowTitle('`ScrollableAreaQt6` demo')
31+
window.setCentralWidget(scrollable_area)
32+
window.show()
3633

37-
scrollable_area = ScrollableAreaQt6()
34+
def single_widget(self, window):
35+
self.win2 = window
36+
window.setWindowTitle('`ScrollableAreaQt5` demo')
37+
window.resize(QSize(600, 200))
3838

39-
vbox = QVBoxLayout(scrollable_area.area)
40-
vbox.addWidget(QLabel(text='big window, small label'))
39+
scrollable_area = ScrollableAreaQt6()
4140

42-
window.setCentralWidget(scrollable_area)
43-
window.show()
44-
app.exec()
41+
vbox = QVBoxLayout(scrollable_area.area)
42+
label = QLabel(text='big window, small label')
43+
vbox.addWidget(label)
44+
45+
window.setCentralWidget(scrollable_area)
46+
window.show()
4547

46-
###############################################################################
4748

4849
if __name__ == '__main__':
49-
grid_of_widgets()
50-
single_widget()
50+
app = QApplication([])
51+
examples = ExamplesScrollableAreaQt6()
52+
app.exec()

examples/examples_ScrollableFrameTk.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,40 @@
66

77
from ScrollableContainers.Tk import ScrollableFrameTk
88

9-
###############################################################################
109

11-
def grid_of_widgets():
12-
root = tk.Tk()
13-
root.title('`ScrollableFrameTk` Demo')
10+
class ExamplesScrollableFrameTk:
1411

15-
# Create a scrollable frame.
16-
scrollable_frame = ScrollableFrameTk(root)
12+
def __init__(self):
13+
self.grid_of_widgets(tk.Tk())
14+
self.single_widget(tk.Toplevel())
1715

18-
# Add widgets to the `frame` attribute of the scrollable frame, not to the
19-
# scrollable frame itself.
20-
dim = 10
21-
for (i, j) in itertools.product(range(dim), repeat=2):
22-
tk.Label(scrollable_frame.frame, text=f'Label\n({i}, {j})').grid(row=i, column=j, padx=10, pady=10)
16+
def grid_of_widgets(self, window):
17+
window.title('`ScrollableFrameTk` Demo')
2318

24-
scrollable_frame.pack(expand=True, fill=tk.BOTH)
25-
root.mainloop()
19+
# Create a scrollable frame.
20+
scrollable_frame = ScrollableFrameTk(window)
2621

27-
###############################################################################
22+
# Add widgets to the `frame` attribute of the scrollable frame, not to
23+
# the scrollable frame itself.
24+
dim = 10
25+
for (i, j) in itertools.product(range(dim), repeat=2):
26+
label = ttk.Label(scrollable_frame.frame, text=f'Label\n({i}, {j})')
27+
label.grid(row=i, column=j, padx=10, pady=10)
2828

29-
def single_widget():
30-
root = tk.Tk()
31-
root.geometry('600x200+50+50')
32-
root.title('`ScrollableFrameTk` Demo')
29+
scrollable_frame.pack(expand=True, fill=tk.BOTH)
3330

34-
scrollable_frame = ScrollableFrameTk(root)
31+
def single_widget(self, window):
32+
window.geometry('600x200+50+50')
33+
window.title('`ScrollableFrameTk` Demo')
3534

36-
tk.Label(scrollable_frame.frame, text='big window, small label').pack()
35+
scrollable_frame = ScrollableFrameTk(window)
3736

38-
scrollable_frame.pack(expand=True, fill=tk.BOTH)
39-
root.mainloop()
37+
label = ttk.Label(scrollable_frame.frame, text='big window, small label')
38+
label.pack()
4039

41-
###############################################################################
40+
scrollable_frame.pack(expand=True, fill=tk.BOTH)
4241

43-
def entry_form_multiple():
44-
root = tk.Tk()
45-
root.title('`ScrollableFrameTk` Demo')
46-
47-
labels = ('Name', 'Age', 'City', 'PIN', 'Occupation', 'Company', 'Profession')
48-
49-
leftframe = ScrollableFrameTk(root)
50-
for (i, label) in enumerate(labels):
51-
tk.Label(leftframe.frame, text=label).grid(row=i, column=0, padx=10, pady=15)
52-
tk.Entry(leftframe.frame).grid(row=i, column=1, padx=10, pady=15)
53-
54-
rightframe = ScrollableFrameTk(root)
55-
for (i, label) in enumerate(reversed(labels)):
56-
tk.Label(rightframe.frame, text=label).grid(row=i, column=0, padx=10, pady=15)
57-
tk.Entry(rightframe.frame).grid(row=i, column=1, padx=10, pady=15)
58-
59-
leftframe.pack(expand=True, fill=tk.BOTH, side=tk.LEFT)
60-
rightframe.pack(expand=True, fill=tk.BOTH, side=tk.RIGHT)
61-
root.mainloop()
62-
63-
###############################################################################
6442

6543
if __name__ == '__main__':
66-
grid_of_widgets()
67-
single_widget()
68-
entry_form_multiple()
44+
examples = ExamplesScrollableFrameTk()
45+
tk.mainloop()

examples/examples_ScrollablePanelWx.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,44 @@
55

66
from ScrollableContainers.Wx import ScrollablePanelWx
77

8-
###############################################################################
98

10-
def grid_of_widgets():
11-
app = wx.App()
12-
root = wx.Frame(None, title='`ScrollablePanelWx` Demo')
9+
class ExamplesScrollablePanelWx:
1310

14-
# Create a scrollable panel.
15-
scrollable_panel = ScrollablePanelWx(root)
11+
def __init__(self):
12+
self.grid_of_widgets(wx.Frame(None))
13+
self.single_widget(wx.Frame(None))
1614

17-
# Add widgets to the `panel` attribute of the scrollable panel, not to the
18-
# scrollable panel itself.
19-
dim = 10
20-
grid_sizer = wx.GridSizer(dim, dim, 20, 20)
21-
for (i, j) in itertools.product(range(dim), repeat=2):
22-
grid_sizer.Add(wx.StaticText(scrollable_panel.panel, label=f'Label\n({i}, {j})'))
15+
def grid_of_widgets(self, window):
16+
window.SetTitle('`ScrollablePanelWx` Demo')
2317

24-
scrollable_panel.panel.SetSizer(grid_sizer)
25-
scrollable_panel.SetupScrolling()
26-
root.Show()
27-
app.MainLoop()
18+
# Create a scrollable panel.
19+
scrollable_panel = ScrollablePanelWx(window)
2820

29-
###############################################################################
21+
# Add widgets to the `panel` attribute of the scrollable panel, not to
22+
# the scrollable panel itself.
23+
dim = 10
24+
grid_sizer = wx.GridSizer(dim, dim, 20, 20)
25+
for (i, j) in itertools.product(range(dim), repeat=2):
26+
text = wx.StaticText(scrollable_panel.panel, label=f'Label\n({i}, {j})')
27+
grid_sizer.Add(text)
3028

31-
def single_widget():
32-
app = wx.App()
33-
root = wx.Frame(None, title='`ScrollablePanelWx` Demo', size=(600, 200))
29+
scrollable_panel.panel.SetSizer(grid_sizer)
30+
scrollable_panel.SetupScrolling()
31+
window.Show()
3432

35-
scrollable_panel = ScrollablePanelWx(root)
33+
def single_widget(self, window):
34+
window.SetTitle('`ScrollablePanelWx` Demo')
35+
window.SetSize(600, 200)
3636

37-
wx.StaticText(scrollable_panel.panel, label='big window, small label')
37+
scrollable_panel = ScrollablePanelWx(window)
3838

39-
scrollable_panel.SetupScrolling()
40-
root.Show()
41-
app.MainLoop()
39+
wx.StaticText(scrollable_panel.panel, label='big window, small label')
40+
41+
scrollable_panel.SetupScrolling()
42+
window.Show()
4243

43-
###############################################################################
4444

4545
if __name__ == '__main__':
46-
grid_of_widgets()
47-
single_widget()
46+
app = wx.App()
47+
examples = ExamplesScrollablePanelWx()
48+
app.MainLoop()

0 commit comments

Comments
 (0)