TOP:由上往下排列,這是預設的排列方式 BOTTOM:由下往上排列 LEFT:由左往右排列 RIGHT:由右往左排列
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.minsize(width=300, height=0)
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.pack(side=LEFT)
label2.pack(side=LEFT)
label3.pack(side=LEFT)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.minsize(width=300, height=0)
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.pack(side=LEFT)
label2.pack(side=LEFT)
label3.pack(side=LEFT)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.minsize(width=300, height=0)
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.pack(side=LEFT, padx=10)
label2.pack(side=LEFT, padx=10)
label3.pack(side=LEFT, padx=10)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.minsize(width=300, height=0)
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.pack(side=LEFT, ipadx=10)
label2.pack(side=LEFT, ipadx=10)
label3.pack(side=LEFT, ipadx=10)
window.mainloop()
none:預設值不延伸 x:水平方向填滿被分配的空間 y:垂直方向填滿被分配的空間 both:水平及垂直方向都填滿被分配的空間
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.minsize(width=300, height=50)
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.pack(side=LEFT, ipadx=10)
label2.pack(side=LEFT, ipadx=10)
label3.pack(side=LEFT, ipadx=10, fill="both")
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.minsize(width=300, height=50)
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.pack(side=LEFT, ipadx=10)
label2.pack(side=LEFT, ipadx=10)
label3.pack(side=LEFT, ipadx=10, fill="both", expand=True)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x20")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.place(x=0, y=0)
label2.place(x=100, y=0)
label3.place(x=200, y=0)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x20")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.place(x=0, y=0, height=20, width=90)
label2.place(x=100, y=0, height=20, width=90)
label3.place(x=200, y=0, height=20, width=90)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.place(relx=0.1, rely=0.4)
label2.place(relx=0.4, rely=0.4)
label3.place(relx=0.7, rely=0.4)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.place(relx=0.1, rely=0.4, x=20, y=20)
label2.place(relx=0.4, rely=0.4, x=20, y=20)
label3.place(relx=0.7, rely=0.4, x=20, y=20)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
label1.place(relx=0.1, rely=0.4, relheight=0.2, relwidth=0.2)
label2.place(relx=0.4, rely=0.4, relheight=0.3, relwidth=0.2)
label3.place(relx=0.7, rely=0.4, relheight=0.4, relwidth=0.2)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
entry1 = Entry()
entry2 = Entry()
label1.grid(row=0, column=1)
label2.grid(row=1, column=0)
label3.grid(row=2, column=0)
entry1.grid(row=1, column=1)
entry2.grid(row=2, column=1)
window.mainloop()
N:對齊格子上方 S:對齊格子下方 E:對齊格子右邊 W:對齊格子左邊
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
entry1 = Entry()
entry2 = Entry()
label1.grid(row=0, column=1, sticky=W)
label2.grid(row=1, column=0)
label3.grid(row=2, column=0)
entry1.grid(row=1, column=1)
entry2.grid(row=2, column=1)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow")
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
entry1 = Entry()
entry2 = Entry()
label1.grid(row=0, column=1, sticky=W+E)
label2.grid(row=1, column=0)
label3.grid(row=2, column=0)
entry1.grid(row=1, column=1)
entry2.grid(row=2, column=1)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow", width=20)
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
entry1 = Entry()
entry2 = Entry()
label1.grid(row=0, column=0, columnspan=2)
label2.grid(row=1, column=0, rowspan=2)
entry1.grid(row=1, column=1)
entry2.grid(row=2, column=1)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow", width=20)
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
entry1 = Entry()
entry2 = Entry()
label1.grid(row=0, column=0, columnspan=2, padx=5, pady=5)
label2.grid(row=1, column=0, padx=5, pady=5)
label3.grid(row=2, column=0, padx=5, pady=5)
entry1.grid(row=1, column=1, padx=5, pady=5)
entry2.grid(row=2, column=1, padx=5, pady=5)
window.mainloop()
from tkinter import *
window = Tk()
window.title("視窗元件配置")
window.geometry("300x100")
label1 = Label(text="label 1", bg="lightyellow", width=20)
label2 = Label(text="label 2", bg="yellow")
label3 = Label(text="label 3", bg="lightgreen")
entry1 = Entry()
entry2 = Entry()
label1.grid(row=0, column=0, columnspan=2, padx=5, pady=5, ipadx=1, ipady=1)
label2.grid(row=1, column=0, padx=5, pady=5, ipadx=1, ipady=1)
label3.grid(row=2, column=0, padx=5, pady=5, ipadx=1, ipady=1)
entry1.grid(row=1, column=1, padx=5, pady=5, ipadx=1, ipady=1)
entry2.grid(row=2, column=1, padx=5, pady=5, ipadx=1, ipady=1)
window.mainloop()
from tkinter import *
# function
def bmi_button_clicked():
# calculate BMI
weight = int(BMI_entry_weight.get())
height = int(BMI_entry_height.get())
if height == 0:
bmi = 0
else:
bmi = round(weight / (height / 100) ** 2, 1)
# BMI interpretation
if bmi == 0:
BMI_label_bmi_out.config(text=f"{bmi}")
BMI_label_category_out.config(text="Error!")
elif bmi < 18.5:
BMI_label_bmi_out.config(text=f"{bmi}")
BMI_label_category_out.config(text="Underweight")
elif bmi < 24:
BMI_label_bmi_out.config(text=f"{bmi}")
BMI_label_category_out.config(text=f"Normal Weight")
elif bmi < 28:
BMI_label_bmi_out.config(text=f"{bmi}")
BMI_label_category_out.config(text="Overweight")
else:
BMI_label_bmi_out.config(text=f"{bmi}")
BMI_label_category_out.config(text=f"Obese")
# window
BMI_window = Tk()
BMI_window.title("BMI calculator")
BMI_window.minsize(width=300, height=200)
BMI_window.config(padx=20, pady=20)
# label
BMI_label_title = Label(width=28, text="BMI calculator", bg="lightyellow", font=("Arial", 12, "normal"))
BMI_label_title.grid(row=0, column=0, columnspan=2, ipadx=2, ipady=2)
BMI_label_height = Label(width=15, text="Height (cm)", bg="yellow", font=("Arial", 10, "normal"))
BMI_label_height.grid(row=1, column=0, padx=5, pady=5)
BMI_label_weight = Label(width=15, text="Weight (kg)", bg="lightgreen", font=("Arial", 10, "normal"))
BMI_label_weight.grid(row=2, column=0, padx=5, pady=5)
BMI_label_bmi = Label(text="BMI:", font=("Arial", 12, "bold"))
BMI_label_bmi.grid(row=4, column=0, padx=5, pady=5)
BMI_label_bmi_out = Label(font=("Arial", 12, "normal"))
BMI_label_bmi_out.grid(row=4, column=1, padx=5, pady=5)
BMI_label_category = Label(text="Category:", font=("Arial", 12, "bold"))
BMI_label_category.grid(row=5, column=0, padx=5, pady=5)
BMI_label_category_out = Label(font=("Arial", 12, "normal"))
BMI_label_category_out.grid(row=5, column=1, padx=5, pady=5)
# entry
BMI_entry_height = Entry(width=20)
BMI_entry_height.grid(row=1, column=1, padx=5, pady=5)
BMI_entry_weight = Entry(width=20)
BMI_entry_weight.grid(row=2, column=1)
# button
BMI_button = Button(width=20, text="Calculate", command=bmi_button_clicked, font=("Arial", 10, "normal"))
BMI_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
BMI_window.mainloop()