One of the advantages of the dynamic programming language Python is the ability to dynamically create a graphical user interface (GUI) at runtime.
Built-in library Tkinter allows you to create, place and move a variety of graphical elements for control, input and output information - widgets.
However, the Tkinter library has one feature, if the number of widgets becomes larger than allows to display the window with the maximum sizes available for your monitor screen, then there is a problem of accessing the hidden area of the window.
The extension of the built-in library Tkinter, the Tix (Tk Interface Extension) library can solve this problem with the help of the ScrolledWindow widget - a window with scrollbars.
The second useful widget that comes with the Tix library is the tabbed window - NoteBook.
However, the use of these widgets in windowed applications in Python has its own peculiarities of displaying information in the case that internal widgets do not fit into the assigned window size.
In this article, we'll look at these features when organizing a dynamic GUI:
The first example is to place the ScrolledWindow widget in the main application window, and thus all other widgets will be automatically placed in it.
The second example is to place the ScrolledWindow widgets only in the tabs of the NoteBook widget.
The examples presented here are implemented in Python 2.7 and the Linux operating system: Ubuntu 16.04LTS.
I hope that implementing these examples in your version of Python and the operating system will not present any complexity for you.
Example No.1 - ScrolledWindow widget as the main container of the entire application.
#!/usr/bin/python
import Tix as tx
root=tx.Tk()
root.geometry("800x600+0+10")
swr=tx.ScrolledWindow(root)
swr.pack(fill=tx.BOTH, expand=1)
nb=tx.NoteBook(swr.window)
nb.pack(fill=tx.BOTH, expand=1)
for i in range(1,21):
nb.add("tab"+str(i),label="Tab "+str(i))
for k in range(1,39):
l=tx.Label(nb.tab1,text="label "+str(k))
l.pack()
for k in range(1,29):
l=tx.Label(nb.tab2,text="label "+str(k))
l.pack(side=tx.LEFT)
root.mainloop()
The result of scenario No.1 can be seen in the following three figures:
Figure 1
The initial view of application No.1 after launch.
It is visible automatic occurrence of scroll bars from the right and in a bottom of the main window.
Since the total size occupied by the NoteBook widget is greater than the original root window (800x600) because of the number of Tabs specified in the first cycle and the number of Labels specified in the next two loops.
This figure shows that of the specified 20 tabs, only 15 were placed in the main window, and only 31 of the 39 labels specified in the first tab are located.
Figure 2
The second tab in the initial state shows only 16 labels out of 28.
The position of the scroll bars remains unchanged.
Figure 3
Moving the bottom scroll bar to the right you can see that the main window covers not only all the tabs of the NoteBook widget but also all the tags placed in the second tab.
Example No.2 - The ScrolledWindow widget is located inside each tab of the NoteBook widget.
#!/usr/bin/python
import Tix as tx
root=tx.Tk()
root.geometry("800x600+0+10")
swm = []
nb=tx.NoteBook(root)
nb.pack(fill=tx.BOTH, expand=1)
for i in range(1,21):
nb.add("tab"+str(i),label="Tab "+str(i))
sw=tx.ScrolledWindow(eval("nb.tab"+str(i)))
swm.append(sw)
sw.pack(fill=tx.BOTH, expand=1)
for k in range(1,39):
l=tx.Label(swm[0].window,text="label "+str(k))
l.pack()
for k in range(1,29):
l=tx.Label(swm[1].window,text="label "+str(k))
l.pack(side=tx.LEFT)
root.mainloop()
The result of scenario No.2 can be seen in the following three figures:
Figure 4
The initial view of the application No.2 after the launch.
The scroll bar appeared only on the right in Tab1 tab, since the number of vertical marks is larger than the original sizes of the main window.
Figure 5
The second tab shows only the bottom scroll bar, since in this tab the labels are horizontally positioned.
Figure 6
Moving the scroll bar to the right, you can see all the labels to the end of the window, but you can not access tabs with numbers greater than 15.
Thus, in variant No.2, the scroll bars provide access only within the tab.
If the number of tabs is greater than what is placed in the horizontal size of the main window, then there will be no access to them.
Therefore, if the number of tabs of the NoteBook widget is not too large to exceed the display limits of the main window, then the considered variant of the location of the ScrolledWindow widget is quite acceptable.
Attempting to unify both variants has yielded the same result as Example No.1, because the widget window of ScrolledWindow expands to the maximum sizes occupied by objects placed in it.
I hope that these examples will help you in designing your window applications in Python.