簡體   English   中英

看不懂 Tkinter 網格系統

[英]Cant understand Tkinter Grid System

我剛開始學習 Tkinter,我正在嘗試制作一個計算器,但按鈕不會像我想要的那樣 position。 我不明白網格的概念,我的行是正確的,但列亂七八糟,並且之間有很大的差距。 我需要幫助修復列,我只是不明白

from tkinter import *

root = Tk()
root.title("Simple Calculator")

input_field = Entry(root, borderwidth = 5, fg = "black", width = 40)

button_1 = Button(root, text = "1")
button_2 = Button(root, text = "2")
button_3 = Button(root, text = "3")
button_4 = Button(root, text = "4")
button_5 = Button(root, text = "5")
button_6 = Button(root, text = "6")
button_7 = Button(root, text = "7")
button_8 = Button(root, text = "8")
button_9 = Button(root, text = "9")
button_0 = Button(root, text = "0")

button_1.grid(row = 1, column = 0)
button_2.grid(row = 1, column = 1)
button_3.grid(row = 1, column = 2)
button_4.grid(row = 2, column = 1)
button_5.grid(row = 2, column = 2)
button_6.grid(row = 2, column = 3)
button_7.grid(row = 3, column = 1)
button_8.grid(row = 3, column = 2)
button_9.grid(row = 3, column = 3)
button_0.grid(row = 4, column = 0)

input_field.grid(row = 0, column = 0, columnspan = 3,  padx = 30, pady = 30)

root = mainloop()

你可以在 grid 中使用 sticky perimeter 你也可以使用 rowconfigure 和 columnconfigure

您可以為按鈕創建不同的框架

你應該閱讀這個https://tkdocs.com/tutorial/grid.html

你可以試試這個:-

from tkinter import*
root = Tk()
root.title("Simple Calculator")

input_field = Entry(root, borderwidth = 5, fg = "black", width = 40)
input_field.pack()
frame = Frame(root)
frame.pack(expand=True,fill=BOTH)
for i in range(1,4):
    frame.rowconfigure(i,weight=1)
for i in range(0,4):
    frame.columnconfigure(i,weight=1)

button_1 = Button(frame, text = "1")
button_2 = Button(frame, text = "2")
button_3 = Button(frame, text = "3")
button_4 = Button(frame, text = "4")
button_5 = Button(frame, text = "5")
button_6 = Button(frame, text = "6")
button_7 = Button(frame, text = "7")
button_8 = Button(frame, text = "8")
button_9 = Button(frame, text = "9")
button_0 = Button(frame, text = "0")

button_1.grid(row = 1, column = 0,sticky=NSEW)
button_2.grid(row = 1, column = 1,sticky=NSEW)
button_3.grid(row = 1, column = 2,sticky=NSEW)
button_4.grid(row = 2, column = 1,sticky=NSEW)
button_5.grid(row = 2, column = 2,sticky=NSEW)
button_6.grid(row = 2, column = 3,sticky=NSEW)
button_7.grid(row = 3, column = 1,sticky=NSEW)
button_8.grid(row = 3, column = 2,sticky=NSEW)
button_9.grid(row = 3, column = 3,sticky=NSEW)
button_0.grid(row = 4, column = 0,sticky=NSEW)



root = mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM