1.グリッドモジュールのパラメーター
グリッドモジュールには多くのパラメーターがあり、一般的に使用されるパラメーターを次の図に示します。
画像はブログから切り取ったものです
サンプルプログラム
以下ではグリッドを計算機ページとして使用します。ターゲットページは下図に示されています
サンプルプログラムは以下のとおりです。
"""计算器软件的界面设计"""
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.creatwidget()
def creatwidget(self):
"""通过grid布局实现计算器界面"""
# 定义一个元组,装计算机的每个按键名称
btnText = (("MC", "M+", "M-", "MR"),
("C", "±", "÷", "×"),
(7, 8, 9, "-"),
(4, 5, 6, "+"),
(1, 2, 3, "="),
(0, "."))
# 第一行显示区域,跨4列,垂直方向外边距为10
Entry(self).grid(row=0, column=0, columnspan=4, pady=10)
# 循环创建按钮
for rindex,r in enumerate(btnText):
for cindex,c in enumerate(r):
if c == "=":
Button(self, text=c, width=3)\
.grid(row=rindex + 1, column=cindex, sticky=NSEW, rowspan=2, padx=1, pady=1)
# sticky=NSEW表示填充整个网格,rowspan表示跨2行
# padx=1, pady=1,表示垂直和水平方向的外边距均为1
elif c == 0:
Button(self, text=c, width=3)\
.grid(row=rindex + 1, column=cindex, sticky=NSEW, columnspan=2, padx=1, pady=1)
# columnspan表示跨两列,
elif c == ".":
Button(self, text=c, width=3)\
.grid(row=rindex + 1, column=cindex+1, sticky=NSEW, padx=1, pady=1)
# 取到"."的时候,将列数+1
else:
Button(self, text=c, width=3)\
.grid(row=rindex+1, column=cindex, sticky=NSEW, padx=1, pady=1)
if __name__ == '__main__':
root = Tk()
root.geometry("200x250")
root.title("计算器")
app = Application(root)
root.mainloop()
3.実行結果
上記はtkinterのgridtモジュールのすべての内容です。質問がある場合は、コメント領域にメッセージを残してください。