I am trying to create a custom widget that includes a separator behind a label. I would like the separator to stretch out behind the label to each side of the window (using grid). I tried to create this myself, but I couldn't get the separator to stick to the edges.
import tkinter as tk
from tkinter import ttk
class LabelSeparator (tk.Frame):
def __init__ (self, parent, text = "", width = "", *args):
tk.Frame.__init__ (self, parent, *args)
self.separator = ttk.Separator (self, orient = tk.HORIZONTAL)
self.separator.grid (row = 0, column = 0, sticky = "ew")
self.label = ttk.Label (self, text = text)
self.label.grid (row = 0, column = 0, padx = width)
if __name__ == "__main__":
root = tk.Tk ()
root.geometry ("200x40")
label = LabelSeparator (root, text = "Label", width = 15)
label.grid (sticky = "ew")
label2 = LabelSeparator (root, text = "A Second Label", width = 15)
label2.grid (sticky = "ew")
root.mainloop ()
The only way I found to expand the separator was to increase the padx on the label, but that doesn't really fix the problem.
I should mention that I'm very new to creating custom widgets.