Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปลี่ยนขนาดข้อความใน Label Widget ของ Tkinter


ภาพรวม

Tkinter Label Widgets ใช้สร้าง Label ในหน้าต่าง สามารถจัด Style Widgets ด้วย Package tkinter.ttk เพื่อปรับ Font-size, Font-family และ Font-style ใช้ Property font('font-family font-style', font-size)

ตัวอย่างโค้ด

# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of frame
win.geometry("650x250")
# Define all the functions
def size_1():
    text.config(font=('Helvetica bold', 20))
def size_2():
    text.config(font=('Helvetica bold', 40))
# Create a Demo Label to which the changes has to be done
text = Label(win, text="Hello World!")
text.pack()
# Create a frame
frame = Frame(win)
# Create a label
Label(frame, text="Select the Font-Size").pack()
# Create Buttons for styling the label
button1 = Button(frame, text="20", command=size_1)
button1.pack(pady=10)
button2 = Button(frame, text="40", command=size_2)
button2.pack(pady=10)
frame.pack()
win.mainloop()

ผลลัพธ์

รันโค้ดจะแสดงหน้าต่างที่มี Text Label ปุ่มสามารถเปลี่ยน Font-size ของ Text Label ได้

วิธีเปลี่ยนขนาดข้อความใน Label Widget ของ Tkinter

เลือกเปลี่ยน Font-size ของ Text Label Widget

วิธีเปลี่ยนขนาดข้อความใน Label Widget ของ Tkinter