Python Tkinter PanedWindow Widget
In this tutorial, we will cover the Tkinter PanedWindow widget which is mainly a container widget containing one or more than one child widgets which are also known as Panes.
This widget arranges child widgets either in a vertical or in a horizontal manner.
It is also known as the Geometry Manager widget.
This widget is used to implement different layouts in a Python desktop application created using the Tkinter module.
The child widgets inside the PanedWindow widget can be resized by the user by moving separator lines sashes using the mouse.
You can implement multiple panes using the PanedWindow widget.
Here is a simple Tkinter application window with three widgets stacked vertically inside a PanedWindow widget.
Tkinter PanedWindow Widget
The syntax of the PanedWindow widget is given below:
W = PanedWindow(master, options)
In the above syntax, the
master parameter denotes the parent window. You can use many options to change the look of the PanedWindow and these options are written as comma-separated.
Tkinter PanedWindow Widget Options:
Following are the various options used with PanedWindow widget:
|Name of the Option||Description|
||This option is used to represent the 3D border size of the widget. The default value of this option indicates that the trough contains no border and the arrowheads and slider contain the 2-pixel border size.|
||This option represents the background color of the widget.|
||This option will convert the mouse pointer to the specified cursor type and it can be set to an arrow, dot, etc.|
||This option is used to indicate the border width of the widget. The default value of this option is 2 pixels.|
||To represents the distance between the handle and the end of the sash we use this option. In horizontal orientation, it is the distance between the top of the sash and the handle. The default value of this option is 8 pixels|
||This option represents the height of the widget. If we do not specify the height then the height will be calculated by the height of the child widgets.|
||This option represents the size of the handle and its default value is 8 pixels. Also, the handle will always be in square|
||The value of this option will be set to HORIZONTAL if we want to place the child windows side by side. If we want to place the child windows from top to bottom then the value of this option will be set to VERTICAL.|
||This option is used to represent the padding to be done around each sash. The default value of this option is 0.|
||This option indicates the width of the sash. The default value of this option is 2 pixels.|
||This option is used to represent the type of border around each of the sash. The default value of this option is FLAT|
||To display the handles, the value of this option should be set to true. The default value of this option is false.|
||This option represents the width of the widget. If we do not specify the height then the height will be calculated by the height of the child widgets.|
||This option indicates the type of border. The default value of this option is FLAT.|
Tkinter PanedWindow Widget Methods:
Following are some methods used with PanedWindow widget:
||This method is mainly used to configure any widget with some specified options.|
||This method is used to get the text at the specified given range.|
||This method is used to add a window to a parent window.|
Tkinter PanedWindow Widget Example
Below we have a basic example for the understanding of the PanedWindow widget. Let us see the code snippet given below:
from tkinter import * # event handler for button def addition(): x = int(e1.get()) y = int(e2.get()) leftdata = str(x+y) leftinput.insert(1, leftdata) # first paned window w1 = PanedWindow() w1.pack(fill=BOTH, expand=1) leftinput = Entry(w1, bd=5) w1.add(leftinput) # second paned window w2 = PanedWindow(w1, orient=VERTICAL) w1.add(w2) e1 = Entry(w2) e2 = Entry(w2) w2.add(e1) w2.add(e2) bottomBtn = Button(w2, text="Addition", command=addition) w2.add(bottomBtn) mainloop()
As you can see above, in the output, we have an application window, in which we have 3 tkinter Entry widgets and 1 tkinter button widget, stacked using 2 PanedWindow widgets packed vertically besides each other.
If you will provide, two numbers in the right side entry widgets and then click on the Addition button, the result of addition of the numbers in the right, will be shown in the entry widget on the left hand side.
Tkinter PanedWindow Widget - Multiple Panes Example
Let us see another code snippet of this widget given below:
from tkinter import * from tkinter import tk win = Tk() pw = PanedWindow(orient ='vertical') #creating Button widget top = tk.Button(pw, text ="Just Click Me!!!\nI am a Button") top.pack(side=TOP) #Adding button widget to the panedwindow pw.add(top) #Creating Checkbutton Widget bot = Checkbutton(pw, text="I am Checkbutton Choose Me!") bot.pack(side=TOP) pw.add(bot) label = Label(pw, text="I am a Label") label.pack(side=TOP) pw.add(label) string = StringVar() entry = Entry(pw, textvariable=string, font=('arial', 15, 'bold')) entry.pack() # This is used to force focus on particular widget # that means widget is already selected for some operations entry.focus_force() pw.add(entry) pw.pack(fill = BOTH, expand = True) # To show sash pw.configure(sashrelief = RAISED) mainloop()
In the above code example, we have create multiple widgets inside a panedwindow widget. We have also used
StringVar() variable and we have used the
focus_force() function to have entry widget in focus when the application is loaded.
In this tutorial, we learned about PanedWindow widget which is a great widget if you want to create multi-column grid like arrangement of widgets in your application.