如何將 Tkinter 文字小部件設為只讀?
在 Tkinter 中,有時,我們可能希望停用文字小部件。要實現這一點,我們可以將文字配置設為 DISABLED。這將會凍結文字小部件並使其變為只讀。
在這個示例中,我們將建立一個文字小部件和一個按鈕,它將允許使用者立即停用或凍結文字小部件。
示例
#Import the library from tkinter import * #Create an instance of window win= Tk() #Set the geometry of the window win.geometry("700x400") def disable_button(): text.config(state= DISABLED) #Label Label(win,text="Type Something",font=('Helvetica bold', 25), fg="green").pack(pady=20) #Create a Text widget text= Text(win, height= 10,width= 40) text.pack() #Create a Disable Button Button(win, text= "Disable", command= disable_button,fg= "white", bg="black", width= 20).pack(pady=20) win.mainloop()
輸出
執行以上程式碼將建立一個文字小部件和一個按鈕,它可以用停用或凍結小部件。
當你點選“停用”按鈕之後,文字小部件將被停用,你將無法在其中輸入任何其他內容。
廣告