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

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


Widget label ใน Tkinter ใช้แสดงข้อความและภาพในแอปพลิเคชัน Tkinter เพื่อเปลี่ยนคุณสมบัติของ label widget เช่น font-property, สี, สีพื้นหลัง, สีหน้า ฯลฯ สามารถใช้เมธอด configure()

หากต้องการเปลี่ยนขนาดข้อความใน Label widget ให้กำหนดคุณสมบัติ font=('font-family font-size style') ใน constructor ของ widget

ตัวอย่าง

# Import the required libraries 
from tkinter import * 
import tkinter.font as tkFont 
# Create an instance of tkinter frame or window 
win=Tk() 
# Set the size of the tkinter window 
win.geometry("700x350") 
def font_style(): 
    label.config(font=('Helvetica bold', 26)) 
# Create a Label 
label = Label(win, text="Click the Button to Change the Font Style.", font=('Times', 24)) 
label.pack() 
b1 = Button(win, text="Change the Label Size", command=font_style) 
b1.pack() 
win.mainloop()

ผลลัพธ์

เมื่อรันโค้ด จะแสดงหน้าต่างพร้อม label widget และปุ่มเปลี่ยนรูปแบบฟอนต์ของ label

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

คลิกปุ่ม "Change the Label Size" จะเปลี่ยนรูปแบบฟอนต์ของ label

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