Python 3 Tkinter:如何创建一条白实线来将小部件与程序的其余部分分开?

Oli*_*Oli 2 python tkinter

我目前正在 Python 3 的 Tkinter 上创建一个健身应用程序。

这是我到目前为止的代码。

import tkinter as tk
from tkinter import *

root = Tk()
root.geometry("1600x1000+0+0")
root.title("Ultimate Fitness Calculator")
root.configure(bg='darkslategray')
lbl_title = tk.Label(root,text="Welcome to the Ultimate Fitness Calculator by Cameron Su.", fg="white", bg = 'darkslategray')
lbl_title.pack()


Tops = Frame(root, width=1600, height=50, bg="darkslategray", relief=SUNKEN)
Tops.pack(side=TOP)

f1 = Frame(root, width=1600, height=900, bg="darkslategray", relief=SUNKEN)
f1.pack(side=LEFT)



lblInfo = Label(Tops, font=('Gill Sans', 50), text="Ultimate Fitness Calculator", fg="white",bg="darkslategray", bd=10, anchor='w').grid(row=0, column=0)

lblInfo = Label(Tops, font=('Gill Sans', 20), text="This multifunctional program calculates Basal Metabolic Rate, Total Daily Energy Expenditures \n and breaks down the amount of macronutrients needed to reach your fitness goals.", fg="white",bg="darkslategray",
                bd=10, anchor='w').grid(row=1, column=0)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

运行代码后,您可以看到它的样子。我想创建一条从左侧到右侧的实心细白线,以便将其与我计划实现的其余代码分开。

鉴于我已经拥有的代码,我该怎么做?

j_4*_*321 5

有一个 ttk 小部件:ttk.Separator(master, orient=..., style=...)。orient 选项是“垂直”或“水平”。

为了让它从左到右填充你的窗口,正如 fhdrsdg 在评论中所说,你可以使用选项打包它 fill='x'

下面是一个例子:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

frame1 = tk.Frame(root)
separator = ttk.Separator(root, orient='horizontal')
frame2 = tk.Frame(root)

frame1.pack(side='top', fill='both', expand=True)
separator.pack(side='top', fill='x')
frame2.pack(side='top', fill='both', expand=True)

tk.Label(frame1, text='This is the top part.').pack()
tk.Label(frame2, text='This is the bottom part.').pack()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

截屏