在Tkinter中,我如何禁用Entry?

时间:2013-11-09 14:17:14

标签: python python-3.x tkinter

我如何在Tkinter中禁用Entry。

def com():
       ....

entryy=Entry()
entryy.pack()

button=Button(text="Enter!", command=com, font=(24))
button.pack(expand="yes", anchor="center")

正如我所说的如何在com功能中禁用Entry?

2 个答案:

答案 0 :(得分:12)

state设为'disabled'

例如:

from tkinter import *

root = Tk()
entry = Entry(root, state='disabled')
entry.pack()
root.mainloop()

from tkinter import *

root = Tk()
entry = Entry(root)
entry.config(state='disabled') # OR entry['state'] = 'disabled'
entry.pack()
root.mainloop()

请参阅Tkinter.Entry.config


因此com函数应为:

def com():
    entry.config(state='disabled')

答案 1 :(得分:3)

如果要一次又一次地更改输入框中的数据,则必须先将其转换为“正常”状态,然后再将数据转换为“禁用”状态

import tkinter as tk
count = 0

def func(en):
    en.configure(state=tk.NORMAL)
    global count
    count += 1
    count=str(count)
    en.delete(0, tk.END)
    text = str(count)
    en.insert(0, text)
    en.configure(state=tk.DISABLED)
    count=int(count)


root = tk.Tk()

e = tk.Entry(root)
e.pack()

b = tk.Button(root, text='Click', command=lambda: func(e))
b.pack()

root.mainloop()