我想在 gui 的行和列之间添加垂直和水平线,最好使用我选择的颜色选项。我尝试四处询问,但没有得到对我有帮助的答案。
我尝试了一些方法,例如 tk 分隔符,但没有运气,我真的很想得到答案,因为这是我遇到的唯一问题。
import tkinter as tk
from tkinter import ttk
# Function to add items to the Treeview
def add_items():
tree.insert("", "end", values=("EUW", "JD123", "Gold"))
tree.insert("", "end", values=("EUNE", "JS456", "Silver"))
tree.insert("", "end", values=("TR", "AJ789", "Bronze"))
# Function to sort by Region column
def sort_by_region():
data = [(tree.set(child, "Region"), child) for child in tree.get_children("") if tree.parent(child) == ""]
data.sort() # Sort the data by Region
for index, (region, child) in enumerate(data):
tree.move(child, "", index) # Move the sorted rows
# Function to sort by Username column
def sort_by_username():
data = [(tree.set(child, "Username"), child) for child in tree.get_children("") if tree.parent(child) == ""]
data.sort() # Sort the data by Username
for index, (username, child) in enumerate(data):
tree.move(child, "", index) # Move the sorted rows
# Function to sort by Rank column
def sort_by_rank():
data = [(tree.set(child, "Rank"), child) for child in tree.get_children("") if tree.parent(child) == ""]
data.sort() # Sort the data by Rank
for index, (rank, child) in enumerate(data):
tree.move(child, "", index) # Move the sorted rows
# Create the main window
root = tk.Tk()
root.title("LOL Account Manager")
root.geometry("740x370")
root.configure(bg="#282e48")
# Create a Frame for the Treeview
frame = tk.Frame(root)
frame.pack(fill="both", expand=True, padx=10, pady=10)
# Create a Treeview with columns
tree = ttk.Treeview(frame, columns=("Region", "Username", "Rank"), show="headings", height=18)
# Define column headings
tree.heading("Region", text="Region", command=sort_by_region)
tree.heading("Username", text="Username", command=sort_by_username)
tree.heading("Rank", text="Rank", command=sort_by_rank)
# Define column widths
tree.column("Region", width=200)
tree.column("Username", width=200)
tree.column("Rank", width=200)
# Add items to the Treeview
add_items()
# Hotkey function for reloading items
def reload_items(event):
for item in tree.get_children():
tree.delete(item)
add_items()
root.bind("<Control-r>", reload_items)
# Configure the style for the Treeview
style = ttk.Style()
style.theme_use("default")
# Configure Treeview's borders for white gridlines
style.configure("Treeview.Heading", background="#282e48", foreground="#FFFFFF")
style.configure("Treeview", background="#282e48", foreground="#FFFFFF", fieldbackground="#282e48", bordercolor="#FFFFFF", borderwidth=1)
style.map("Treeview.Heading", background=[('active', '#282e48'), ('!active', '#282e48')]) # Prevent color change on hover
# Pack the Treeview
tree.pack(fill="both", expand=True)
# Update the window to get proper dimensions
root.update_idletasks()
# Calculate the screen width and height
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# Calculate the position to center the window
x_coordinate = int((screen_width - root.winfo_width()) / 2)
y_coordinate = int((screen_height - root.winfo_height()) / 2)
# Set the window to be centered
root.geometry(f"+{x_coordinate}+{y_coordinate}")
root.mainloop()
在
Treeview
小部件之后添加此内容:
片段:
vsb = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
vsb.pack(side='right', fill='y')
hsb = ttk.Scrollbar(frame, orient="horizontal", command=tree.xview)
hsb.pack(side='bottom', fill='x')
tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)