我对(自定义)tkinter 很陌生,想知道如何创建表并在其中放置数据。数据是我在画布中创建的点的坐标,该数据也存储在列表中。应该可以读取表格并更改其中的值。如果可以创建这种没有类的表,那就太好了。 现在我创建了一个文本框,其中存储了一个坐标,但我在读取该文本框中的值时遇到问题。
谢谢您的帮助。
import customtkinter as tk
tk.set_appearance_mode("light")
tk.set_default_color_theme("dark-blue")
root = tk.CTk()
root.geometry("800x900")
root.title("The test GUI")
first_colour = "lime green"
second_colour = "navy"
def join_polygon1(event):
canvas.create_line(points1[0], points1[1], points1[-2], points1[-1], width=3, fill= first_colour)
frame1 = tk.CTkFrame(master=root)
frame1.pack(padx=10, pady=10, fill="both", expand=True)
title = tk.CTkLabel(master=frame1, text="Polygon creation GUI", font=("Roboto", 24))
title.pack()
canvas = tk.CTkCanvas(frame1, width=600, height=350, bg="white")
canvas.pack(pady=10)
global points1
points1 = []
frame = tk.CTkFrame(frame1)
frame.columnconfigure(1, minsize=120)
frame.columnconfigure(2, minsize=120)
x_coor1 = tk.CTkLabel(frame, text="x coor. polygon 1 |", text_color=first_colour)
x_coor1.grid(row=0, column=1)
y_coor1 = tk.CTkLabel(frame, text="y coor. polygon 1 ", text_color=first_colour)
y_coor1.grid(row=0, column=2)
def polygon1(event):
global first_polygon
x1 = event.x
y1 = event.y
r = 5
circle1 = canvas.create_oval(x1, y1, x1 + r, y1 + r, fill=first_colour, outline=first_colour)
points1.append(x1)
points1.append(y1)
frame = tk.CTkFrame(frame1)
frame.columnconfigure(1, minsize=100)
frame.columnconfigure(2, minsize=100)
text1 = tk.CTkTextbox(frame, height=1, width=90, text_color=first_colour)
text1.grid(row=0, column=1)
text1.insert("0.0", str(points1[-2]))
text2 = tk.CTkTextbox(frame, height=1, width=90, text_color=first_colour)
text2.grid(row=0, column=2)
text2.insert("0.0", str(points1[-1]))
frame.pack()
if len(points1) > 2:
first_polygon = canvas.create_line(points1, width=3, fill=first_colour)
root.bind("<Return>", join_polygon1)
frame.pack()
canvas.bind("<Button-1>", polygon1)
canvas.bind("<Button-3>", polygon2)
root.mainloop()
您可以使用
Treeview
包中的 tkinter
小部件并将其包含在您的 customtkinter
框架中,如下所示:
headers2=['#', 'Name']
_tree=tk.ttk.Treeview(master=frameCustom,height=25,columns=headers2,show='headings')
_tree.heading('#', text='#')
_tree.heading('Name', text='Name')
_tree.pack()
cursor = ((u"1", u"Name A"),
(u"2", u"Name B"))
for row in cursor:
_tree.insert('', 'end', values=row)
for i, item in enumerate(row):
_tree.column(headers2[i],width=col_width,anchor=tk.CENTER)