如何在 python 中制作网格并将其居中?
How to make a grid in python and center it?
所以我正在尝试制作一个可以输入成绩的网格
我的代码目前看起来像这样
这是一个程序的图片,我想制作一个网格,这样每个月和每个类别都有一个条目,就像成绩簿一样:
代码示例
import tkinter as tk
window8 = tk.Tk()
window8.title('Grades')
window8.geometry('1920x1080')
lbl_mjeseciime = tk.Label(master=window8, text='Months')
lbl_mjeseciime.place(x=1000, y=100)
lbl_kategorije = tk.Label(text='Categories:', master=window8)
lbl_kategorije.place(x=450, y=200)
lbl_mjeseci = tk.Label(master=window8,text='9 10 11 12 1 2 3 4 5 6')
lbl_mjeseci.place(x=700, y=200)
lbl_kategorija1 = tk.Label(master=window8, text='Category1')
lbl_kategorija2 = tk.Label(master=window8, text='Category2')
lbl_kategorija1.place(x=450, y=300)
lbl_kategorija3 = tk.Label(master=window8, text='Category3')
lbl_kategorija2.place(x=450, y=450)
lbl_kategorija3.place(x=450, y=600)
window8.mainloop()
您可以使用如下内容:
root = tk.Tk()
titles = ["Months", 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
number_of_months = 10
categories = ["Category1", "Category2", "Category3"]
entries = []
# Create headers for months
for n, t in enumerate(titles):
tk.Label(root, text = str(t)).grid(row = 0, column = n)
# Create rows for categories
for n, r in enumerate(categories):
# Create category text
tk.Label(root, text = r).grid(row = n+1, column = 0)
# Create list to store entries
row_entries = []
# Create an entry for each month
for m in range(number_of_months):
e = tk.Entry(root)
row_entries.append(e)
e.grid(row = n+1, column = m+1)
entries.append(row_entries)
root.mainloop()
我没有手动创建和放置每个 Label
,而是将它们放在一个循环中。我使用 grid
来放置小部件,因为它允许您轻松地将小部件放置在 row/column 中。我还将所有条目存储在二维列表中 entries
以便您可以在某个时候获取它们的值。
所以我正在尝试制作一个可以输入成绩的网格 我的代码目前看起来像这样
这是一个程序的图片,我想制作一个网格,这样每个月和每个类别都有一个条目,就像成绩簿一样:
代码示例
import tkinter as tk
window8 = tk.Tk()
window8.title('Grades')
window8.geometry('1920x1080')
lbl_mjeseciime = tk.Label(master=window8, text='Months')
lbl_mjeseciime.place(x=1000, y=100)
lbl_kategorije = tk.Label(text='Categories:', master=window8)
lbl_kategorije.place(x=450, y=200)
lbl_mjeseci = tk.Label(master=window8,text='9 10 11 12 1 2 3 4 5 6')
lbl_mjeseci.place(x=700, y=200)
lbl_kategorija1 = tk.Label(master=window8, text='Category1')
lbl_kategorija2 = tk.Label(master=window8, text='Category2')
lbl_kategorija1.place(x=450, y=300)
lbl_kategorija3 = tk.Label(master=window8, text='Category3')
lbl_kategorija2.place(x=450, y=450)
lbl_kategorija3.place(x=450, y=600)
window8.mainloop()
您可以使用如下内容:
root = tk.Tk()
titles = ["Months", 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
number_of_months = 10
categories = ["Category1", "Category2", "Category3"]
entries = []
# Create headers for months
for n, t in enumerate(titles):
tk.Label(root, text = str(t)).grid(row = 0, column = n)
# Create rows for categories
for n, r in enumerate(categories):
# Create category text
tk.Label(root, text = r).grid(row = n+1, column = 0)
# Create list to store entries
row_entries = []
# Create an entry for each month
for m in range(number_of_months):
e = tk.Entry(root)
row_entries.append(e)
e.grid(row = n+1, column = m+1)
entries.append(row_entries)
root.mainloop()
我没有手动创建和放置每个 Label
,而是将它们放在一个循环中。我使用 grid
来放置小部件,因为它允许您轻松地将小部件放置在 row/column 中。我还将所有条目存储在二维列表中 entries
以便您可以在某个时候获取它们的值。