所以我正在嘗試制作一個可以輸入成績的網格我的代碼目前看起來像這樣
這是一個程式的圖片,我想制作一個網格,以便每個月和每個類別都有一個條目,類似于成績簿:

代碼示例
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()
uj5u.com熱心網友回復:
您可以使用以下內容:
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放置小部件,因為它允許您輕松地將小部??件放置在行/列中。我還將所有條目存盤在一個二維串列中entries,以便您可以在某個時候獲取它們的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483736.html
下一篇:下拉選單python
