In this Python tutorial, we will learn how to use the Python Tkinter Separator widget. Also, we will cover these topics:
- Python Tkinter Separator
- Python Tkinter Separator Grid
- Python Tkinter Separator Example
- Python Tkinter Separator Color
- Python Tkinter Separator Width
- Python Tkinter Menu Add Separator
- Python Tkinter Frame Separator
- Python Tkinter Vertical Separator
Python Tkinter Separator
Writing software with a good presentation is important because that saves the user’s time which ultimately contributes to the popularity of the software. Python Tkinter Separator is used to group the widgets on the same screen or window.
This way all the widgets look organized in the window and users feel comfortable using the application.
Syntax:-
Below is the syntax to create a separator in Python Tkinter. Although we have mentioned all the available options, the only master is the mandatory parameter required to create a separator in Python Tkinter.
ttk.Separator(
master=ws,
orient=HORIZONTAL,
style='TSeparator',
class_= ttk.Separator,
takefocus= 1,
cursor='plus'
).pack(fill=X, padx=10, expand=True)
Parameters description:-
- master: the window name where you want to place the widget. It could be a parent or child window.
- orient: [HORIZONTAL | VERTICAL] make sure to adjust the value for the fill option to the geometry manager accordingly. we have set fill=X because orientation is horizontal.
- style: you can customize the background color of the separator widget using style. The default style is TSeparator.
- class_: you can specify the name of the class whose property you want to inherit in the program. the widget name is the default class name.
- takefocus: [0 | 1 ] if takefocus is set to 0 then the separator widget won’t take focus and vice versa.
- cursor: icon of the mouse cursor changes every time it hovers over the separator widget. (
- master: the window name where you want to place the widget. It could be a parent or child window.
- orient: [HORIZONTAL | VERTICAL] make sure to adjust the value for the fill option to the geometry manager accordingly. we have set fill=X because orientation is horizontal.
- style: you can customize the background color of the separator widget using style. The default style is TSeparator.
- class_: you can specify the name of the class whose property you want to inherit in the program. the widget name is the default class name.
- takefocus: [0 | 1 ] if takefocus is set to 0 then the separator widget won’t take focus and vice versa.
- cursor: icon of the mouse cursor changes every time it hovers over the separator widget. (arrow, circle, clock, cross, dotbox, exchange, fleur, heart, man, mouse, pirate, plus, shutle, sizing, spider, spraycan, star, target,tcross, trek, watch).
Example:-
In the below example, we have created two separators (horizontal and vertical).
# modules
from tkinter import ttk
from tkinter import *
# instance of object
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x400')
# create styling
styl = ttk.Style()
styl.configure('TSeparator', background='grey')
# horizontal separator
ttk.Separator(
master=ws,
orient=HORIZONTAL,
style='TSeparator',
class_= ttk.Separator,
takefocus= 1,
cursor='plus'
).pack(fill=X, padx=10, expand=True)
# vertical separator
ttk.Separator(
master=ws,
orient=VERTICAL,
style='TSeparator',
class_= ttk.Separator,
takefocus= 1,
cursor='man'
).pack(fill=Y, pady=10, expand=True)
# infinite loop
ws.mainloop()
In the below output, you can see that horizontal and vertical separators are created using Python Tkinter.
Read: Python Tkinter Label
Python Tkinter Separator Grid
Grid is a geometry manager that positions the widgets in a row and column format. This allows organizing app widgets in a tabular format.
Before we jump into how to create a separator in Python Tkinter, let’s revise the basic concepts of the grid in python Tkinter:
- Rows are the horizontal grid(s) and columns are the vertical grid(s).
- columnspan and rowspan in a grid allow to merge of two or more columns and rows but those columns must be created or occupied. If you have created a grid till 5 rows or columns and you are trying to merge more than that then it will occupy only 5 rows or columns.
- Sticky allows to stretch the grid in any direction.
- padx and pady create a space outside the widget.
- ipadx and ipady created a space within the widget.
Using the ipadx or ipady option in grid geometry manager we can separator widget in Python Tkinter. The ipadx will be used for the horizontal separator whereas pady will be used for the vertical separator.
Example:-
In the below example, we have demonstrated how to create a Python Tkinter Separator widget using grid geometry manager. We have also displayed the various options that we tried but didn’t work as a comment.
# modules
from tkinter import ttk
from tkinter import *
# create window
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x400')
# style
styl = ttk.Style()
styl.configure('blue.TSeparator', background='blue')
styl.configure('red.TSeparator', background='red')
# horizontal separator
ttk.Separator(
master=ws,
orient=HORIZONTAL,
style='blue.TSeparator',
class_= ttk.Separator,
takefocus= 1,
cursor='plus'
).grid(row=0, column=1, ipadx=200, pady=10)
# ways that didn't worked
#).grid(row=1,column=0,columnspan=4, sticky="ew", padx=10, pady=10)
#).grid(row=0, columnspan=5, sticky=EW)
#).pack(fill=X, padx=10, expand=True)
# vertical separator
ttk.Separator(
master=ws,
orient=VERTICAL,
style='red.TSeparator',
class_= ttk.Separator,
takefocus= 1,
cursor='man'
).grid(row=1, column=1, ipady=200, padx=10, pady=10)
# infinite loop
ws.mainloop()
Here is the output of the above code, here blue line represents the horizontal separator whereas the red line represents the vertical separator.
Read: Python Tkinter Canvas Tutorial
Python Tkinter Separator Line Example
In this section, we will create a project that has a separator widget in Python Tkinter. We have created signup and login forms on the same window and both the things are separated using a Python Tkinter separator.
from tkinter import *
from tkinter import ttk
ws = Tk()
ws.geometry('700x400')
ws.title('PythonGuides')
f = ('Helvetica', 14)
Label(
ws,
text='Full name',
font=f
).place(x=10, y=10)
Entry(ws, font=f).place(x=100, y=10)
Label(
ws,
text='Email',
font=f
).place(x=10, y=40)
Entry(ws, font=f).place(x=100, y=40)
Label(
ws,
text='Mobile',
font=f
).place(x=10, y=70)
Entry(ws, font=f).place(x=100, y=70)
genwin = LabelFrame(
ws,
text='Date of birth',
font=f
)
genwin.place(x=10, y=100)
Radiobutton(
genwin,
text='Male',
value=1,
font=f
).pack(side=LEFT)
Radiobutton(
genwin,
text='Female',
value=2,
font=f
).pack(side=LEFT)
Radiobutton(
genwin,
text='Others',
value=3,
font=f
).pack(side=LEFT)
dobwin = LabelFrame(
ws,
text='Gender',
font=f
)
dobwin.place(x=10, y=170)
ttk.Combobox(
dobwin,
values=['01', '02', '03', '04', '05', '06' ],
width=5,
font=f
).pack(side=LEFT)
ttk.Combobox(
dobwin,
values=['Janaury', 'February', 'March', 'April', 'May', 'June' ],
width=7,
font=f
).pack(side=LEFT)
ttk.Combobox(
dobwin,
values=['1995', '1996', '1997', '1998', '1999', '2000' ],
width=5,
font=f
).pack(side=LEFT)
Checkbutton(
ws,
text= 'Accept Terms & Conditions',
).place(x=10, y=240)
Button(
ws,
text='Submit',
command=None,
font=f,
width=10
).place(x=10, y=270)
Button(
ws,
text='Clear',
command=None,
font=f,
width=10
).place(x=150, y=270)
ttk.Separator(
ws,
takefocus=0,
orient=VERTICAL
).place(x=350, y=0, relheight=1)
logwin = LabelFrame(
ws,
text='Login',
font=f,
padx=10,
pady=10
)
logwin.place(x=360, y=10)
Label(
logwin,
text='Email',
font=f,
pady=10
).grid(row=0, column=0, sticky='w')
Entry(
logwin,
font=f,
width=15
).grid(row=0, column=1)
Label(
logwin,
text='Password',
font=f
).grid(row=1, column=0)
Entry(
logwin,
font=f,
width=15
).grid(row=1, column=1)
Button(
logwin,
text='Submit',
command=None,
font=f
).grid(row=2, column=1, sticky=EW, pady=10)
ws.mainloop()
In the below output, we have used the separator widget to organize the register and login sections.
Read: Python Tkinter Listbox
Python Tkinter Separator Color
Python Tkinter Separator provides an option style using which we can change the color of the separator line.
The default name of the separator widget style is TSeparator but you can create your customized name by adding a new name followed by a dot(.) and the old name which is TSeparator in Python Tkinter.
And later this new name can be used to add style to the Python Tkinter Separator widget. Here is an example of creating a new name :
styl = ttk.Style()
styl.configure('blue.TSeparator', background='blue')
In the above script, blue.TSeperator is a name created and the background color is assigned as blue. Now let’s see how we can use this style in our program.
ttk.Separator(
ws,
orient=HORIZONTAL,
style='blue.TSeparator'
).pack()
Here is the output of the above code, here a blue color separator is created across the window in Python Tkinter.
You can create multiple styles and use them as and when required in the program. Here is an example to demonstrate the use of multiple colors in the Python Tkinter separator.
Source Code:
In this code, we have created multiple styles with different background colors for the Python Tkinter separator. changing the style name will change the color of the separator.
from tkinter import *
from tkinter import ttk
ws = Tk()
ws.title('PythonGuides')
ws.geometry('300x200+800+200')
styl = ttk.Style()
styl.configure('blue.TSeparator', background='blue')
styl.configure('red.TSeparator', background='red')
styl.configure('green.TSeparator', background='green')
styl.configure('yellow.TSeparator', background='yellow')
ttk.Separator(
ws,
orient=HORIZONTAL,
style='yellow.TSeparator'
).pack(fill=X, expand=True)
ws.mainloop()
Output:
In the below output, we have demonstrated the Python Tkinter Separator color. The color changes every time the style type is changed in Python Tkinter.
Read: Python Tkinter Frame
Python Tkinter Separator Width
The separator widget in Python Tkinter does not provide any option to change its width but it does provide a class_ option. This means you can create a class using and then provide the name of it here.
In your customized class you can perform other activities as well like assigning height, width, border, etc to the separator.
We see that method as a workaround and to get help regarding that you refer to the following webpage.
Python Tkinter Menu Add Separator
The menu bar in Python Tkinter is used to display a menubar on the Tkinter window. Adding Separator to the menu items shows the grouping of similar items.
Please note that this separator is different from the separator we have discussed in the entire blog. This section is to give clearance about the section that is used only in a menubar between menu items.
The image below shows the example of the menu bar in Python Tkinter and the use of a separator in the popular notepad application.
The separators can be added to the menu bar using the add_separator() method.
Source code:–
In the below source code, we have created a menu bar and added a separator before print setup and after print menu items.
# modules
from tkinter import *
# create window
ws =Tk()
ws.title("Python Guides")
ws.geometry("300x200")
# create menu
menubar = Menu(ws)
# use menu
file = Menu(menubar)
# add menu items
file.add_command(label='New')
file.add_command(label='New Window')
file.add_command(label='Open...')
file.add_command(label='Save')
file.add_command(label='Save As...')
# add Separator
file.add_separator()
file.add_command(label='Print Setup...')
file.add_command(label='Print...')
# add separator
file.add_separator()
file.add_command(label='Exit')
file.add_command(label="Exit", command=ws.quit)
menubar.add_cascade(label="File", menu=file)
# add menu on the screen
ws.config(menu=menubar)
# infinite loop
ws.mainloop()
Here is the output of the above code, here you can see a menu bar with separators in Python Tkinter.
Read: Python Tkinter Menu Bar
Python Tkinter Frame Separator
Frames are the container widgets that are used to store other widgets over them. Separators and frames have one thing in common they both are used to organize the other widgets on the window.
Since we have a separator in Python Tkinter so it is not advised to use the frame to perform separation on the Tkinter application. But since it is asked, we will show you how it can be done.
The solution is just a workaround to make the frame look and act like a separator widget in Python Tkinter.
Source code:-
In the below source code, we have created two frames that look identical to the separator widget in Python Tkinter. The orange line is the horizontal line and the green is the vertical line created using the frame widget in Python Tkinter.
from tkinter import *
from tkinter import ttk
ws =Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
frame = Frame(
ws,
bg='orange',
)
frame.pack(fill=X, expand=True)
frame2 = Frame(
ws,
bg='green',
)
frame2.pack(fill=Y, expand=True)
ws.mainloop()
Output:-
In the below output, you can see there are two separators created using the frame widget in Python Tkinter.
Read: Python Tkinter Checkbutton
Python Tkinter Vertical Separator
Separators can be aligned in horizontal and vertical positions on the window. The default position is horizontal. In this section, we will learn how to create a vertical separator in Python Tkinter.
Vertical separators can be created by providing the orient value to VERTICAL. All the words must be in upper case. In case you want to use lowercase then put it inside the quotations (‘vertical’).
While applying geometry manager make sure to apply the following settings for different geometry managers (pack, grid & place) in Python Tkinter:
- Pack(fill=Y, expand=True) :- this will stretch the separator in the vertical direction.
- Grid(ipady=100) :- ipady creates a space inside the widget vertically.
- Place(relheight=1) :- Relative height will stretch the separator in vertical position.
Example:-
In the below example, we have demonstrated how to create a vertical separator using all the geometry managers. Since we cannot use all the geometry managers on the same window that is why we have used the container widget frame.
# modules
from tkinter import *
from tkinter import ttk
# create window
ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
# frames to store separators with different geometry managers
frame1 = Frame(ws)
frame1.pack(side=LEFT, expand=True, fill=BOTH)
frame2 = Frame(ws)
frame2.pack(side=LEFT, expand=True, fill=BOTH)
frame3 = Frame(ws)
frame3.pack(side=LEFT, expand=True, fill=BOTH)
# style to add colors on separators
styl = ttk.Style()
styl.configure('red.TSeparator', background='red')
styl.configure('blue.TSeparator', background='blue')
styl.configure('yellow.TSeparator', background='yellow')
# vertical separator with pack geometry manager
ttk.Separator(
frame1,
orient=VERTICAL,
style='blue.TSeparator'
).pack(fill=Y, expand=True)
# vertical separator with grid geometry manager
ttk.Separator(
frame2,
orient=VERTICAL,
style='red.TSeparator'
).grid(row=0, column=0, ipady=200)
# vertical separator with place geometry manager
ttk.Separator(
frame3,
orient=VERTICAL,
style='yellow.TSeparator'
).place(x=10, y=0, relheight=1)
# infinite loop
ws.mainloop()
The below output shows a vertical separator widget in Python Tkinter. Each line is created on different geometry managers.
Also, take a look at some more Python Tkinter tutorials.
- Python Tkinter Radiobutton
- Python Tkinter Map() Function
- Python Tkinter Exit Program
- Python Tkinter ToDo List
- Expense Tracker Using Python Tkinter
In this tutorial, we have learned how to use the Python Tkinter Separator widget. Also, we have covered these topics:
- Python Tkinter Separator
- Python Tkinter Separator Grid
- Python Tkinter Separator Example
- Python Tkinter Separator Color
- Python Tkinter Separator Width
- Python Tkinter Menu Add Separator
- Python Tkinter Frame Separator
- Python Tkinter Vertical Separator
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.