[Pythjon] レイアウト

in hive-101145 •  26 days ago 

こんにちは、@yasuです。

今回はレイアウトについて学習してみたいと思います。

いくつかのレイアウトのテンプレートみたいなものを作ってみました。

実行環境
今回は、https://repl.it/ ではなく Visual Studio Codeで行います。

コード

from tkinter import *

root = Tk()
root.title("Python Test")
root.geometry("640x400")

#
# 上下左右は固定、中央は可変
#
def layout1():
    btn_n = Button(root, text="上",height=2)
    btn_n.pack(side="top",fill="x")

    btn_s = Button(root, text="下",height=2)
    btn_s.pack(side="bottom",fill="x")

    btn_e = Button(root, text="右",width=10)
    btn_e.pack(side="right",fill="y")

    btn_w = Button(root, text="左",width=10)
    btn_w.pack(side="left",fill="y")

    btn_center = Button(root, text="中央")
    btn_center.pack(fill="both",expand = True)

layout1()

root.mainloop()

実行結果

image.png

そのほかのレイアウト例

#
# 左50%、右に50%
#
def layout2():
    btn_e = Button(root, text="右")
    btn_e.pack(side="right",fill="both",expand = True)

    btn_w = Button(root, text="左")
    btn_w.pack(side="left",fill="both",expand = True)

image.png

#
# 左は固定、右は可変
#
def layout21():
    btn_e = Button(root, text="右",width=10)
    btn_e.pack(side="right",fill="both",anchor="w")

    btn_w = Button(root, text="左")
    btn_w.pack(side="left",fill="both",anchor="e",expand = True)

image.png

#
# 左右は固定、中央は可変
#
def layout22():
    btn_e = Button(root, text="右",width=10)
    btn_e.pack(side="right",fill="both",anchor="w")

    btn_w = Button(root, text="左",width=10)
    btn_w.pack(side="left",fill="both",anchor="e")

    btn_center = Button(root, text="中央")
    btn_center.pack(fill="both",expand = True)

image.png

#
# 上50%、下に50%
#
def layout3():

    btn_n = Button(root, text="上",height=2)
    btn_n.pack(side="top",fill="both",expand = True)

    btn_s = Button(root, text="下",height=2)
    btn_s.pack(side="bottom",fill="both",expand = True)

image.png

#
# 上は可変、下は固定
#
def layout31():

    btn_n = Button(root, text="上")
    btn_n.pack(side="top",fill="both",expand = True)

    btn_s = Button(root, text="下",height=2)
    btn_s.pack(side="bottom",fill="both")

image.png

#
# 上下は固定、中央は可変
#
def layout32():

    btn_n = Button(root, text="上",height=2)
    btn_n.pack(side="top",fill="both")

    btn_s = Button(root, text="下",height=2)
    btn_s.pack(side="bottom",fill="both")

    btn_center = Button(root, text="中央")
    btn_center.pack(fill="both",expand = True)

image.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@yasu transfered 0.5 KRWP to @krwp.burn. voting percent : 5.68%, voting power : 62.25%, steem power : 1924888.04, STU KRW : 1200.
@yasu staking status : 250 KRWP
@yasu limit for KRWP voting service : 0.5 KRWP (rate : 0.002)
What you sent : 0.5 KRWP [47249535 - 083d378af584b28fc378792f06eb5b2e99415d1f]