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

วิธีเปลี่ยน State ของ Tkinter Button จาก Disabled เป็น Normal


ภาพรวม

Tkinter มี Button Widgets สร้างปุ่ม Trigger Event สมมติมีปุ่มที่ Disable อยู่แล้ว ต้องการเปลี่ยน State ใช้ Property state ใช้ Enable/Disable ปุ่ม มี 2 Operation: state=DISABLED และ state=NORMAL

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

# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of frame
win.geometry("650x450")
# Define a function for Button Object
def quit_win():
    win.destroy()
# Create a button to quit the window
Button(win, text="Quit", command=quit_win, font=('Helvetica bold', 20), state=NORMAL).pack(pady=5)
win.mainloop()

Output 1 (Disabled)

Output เมื่อปุ่ม Disabled

วิธีเปลี่ยน State ของ Tkinter Button จาก Disabled เป็น Normal

Output 2 (Normal)

Output เมื่อ Button state=NORMAL

วิธีเปลี่ยน State ของ Tkinter Button จาก Disabled เป็น Normal