I am making a login software and I want the input fields to have only the bottom border like we have in CSS. Is there any way to do this in tkinter
?
Advertisement
Answer
There are a few ways to do this. Arguably the simplest is to turn off the border, and then use place
to add a separator. You can wrap that up in a class so that you can use it just like a normal entry.
Here’s a simple example:
JavaScript
x
23
23
1
import tkinter as tk
2
from tkinter import ttk
3
4
class CustomEntry(tk.Entry):
5
def __init__(self, *args, **kwargs):
6
kwargs["borderwidth"] = 0
7
super().__init__(*args, **kwargs)
8
separator = ttk.Separator(orient="horizontal")
9
separator.place(in_=self, x=0, rely=1.0, height=2, relwidth=1.0)
10
11
root = tk.Tk()
12
style = ttk.Style()
13
print(style.layout("TEntry"))
14
print(style.layout("TSeparator"))
15
16
for row, field in enumerate(("First", "Last")):
17
label = tk.Label(root, text=field)
18
entry = CustomEntry(root, width=20, highlightthickness=0)
19
label.grid(row=row, column=0, sticky="e", padx=10, pady=4)
20
entry.grid(row=row, column=1, sticky="ew", padx=10, pady=4)
21
22
root.mainloop()
23