In Tkinter, How I disable Entry?

我的未来我决定 提交于 2021-02-16 06:05:59

问题


How I disable Entry in Tkinter.

def com():
       ....

entryy=Entry()
entryy.pack()

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

As I said How I disable Entry in com function?


回答1:


Set state to 'disabled'.

For example:

from tkinter import *

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

or

from tkinter import *

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

See Tkinter.Entry.config


So the com function should read as:

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



回答2:


if we want to change again and again data in entry box we will have to first convert into Normal state after changing data we will convert in to disable state

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()


来源:https://stackoverflow.com/questions/19876992/in-tkinter-how-i-disable-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!