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?
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')
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()
Related
I am new to tkinter and learning to use the widget "CheckButton".
I have encountered a problem, whatever I select, the code prints 0.
Wanted:
print 1 if selected
print 0 if not selected
MWE
import tkinter as tk
win = tk.Tk()
win.title('My Window')
win.geometry('100x100')
l = tk.Label(win, width=20, text='MultipleReplace')
l.pack()
var_cb1 = tk.IntVar()
var_cb1.set(0)
cb1 = tk.Checkbutton(win, text='Yes',variable=var_cb1,onvalue=1,offvalue=0)
cb1.pack(side=tk.TOP)
var_cb1_val = var_cb1.get()
print('The selected value is : ', var_cb1_val)
win.mainloop()
You're calling get on var_cb1 pretty much immediately after setting it to 0, and then printing that value. You're not waiting for the checkbutton to change states and then printing the value. Define a function that gets called when the CheckButton changes state, then get the value there
import tkinter as tk
def on_update(var):
print('The selected value is: ', var.get()) # get the new value
win = tk.Tk()
win.title('My Window')
win.geometry('100x100')
l = tk.Label(win, width=20, text='MultipleReplace')
l.pack()
var_cb1 = tk.IntVar()
var_cb1.set(0)
cb1 = tk.Checkbutton(
win,
text='Yes',
variable=var_cb1,
onvalue=1,
offvalue=0,
# call 'on_update', using a lambda to pass in the 'var_cb1' variable
command=lambda var=var_cb1: on_update(var)
)
cb1.pack(side=tk.TOP)
win.mainloop()
If i have this ENTRY label1 on pos1, how can i update and show "in real time" the text i write on other label 2 in position2?
label1 = Entry(root, font=('aria label', 15), fg='black')
label1.insert(0, 'enter your text here')
label1_window = my_canvas.create_window(10, 40, window=entry)
label2 = how to update and show in real time what user write on label1
If the entry and label use the same StringVar For the textvariable option, the label will automatically show whatever is in the entry. This will happen no matter whether the entry is typed in, or you programmatically modify the entry.
import tkinter as tk
root = tk.Tk()
var = tk.StringVar()
canvas = tk.Canvas(root, width=400, height=200, background="bisque")
entry = tk.Entry(root, textvariable=var)
label = tk.Label(root, textvariable=var)
canvas.pack(side="top", fill="both", expand=True)
label.pack(side="bottom", fill="x")
canvas.create_window(10, 40, window=entry, anchor="w")
root.mainloop()
Issues in your attempt: The variable names are not clear as you are creating an Entry component and then assigning it to a variable named label1 which can be confusing.
Hints: You can do one of the following to tie the label to the entry so that changing the text in the entry causes the text in the label to change:
Use a shared variable
Implement a suitable callback function. You can, for example, update the label each time the KeyRelease event occurs.
Solution 1 - Shared variable: Below is a sample solution using a shared variable:
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title('Example')
root.geometry("300x200+10+10")
user_var = StringVar(value='Enter text here')
user_entry = Entry(root, textvariable=user_var, font=('aria label', 15), fg='black')
user_entry.pack()
echo_label = Label(root, textvariable=user_var)
echo_label.pack()
root.mainloop()
Solution 2 - Callback function: Below is a sample solution using a suitable callback function. This is useful if you wish to do something more:
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title('Example')
root.geometry("300x200+10+10")
def user_entry_changed(e):
echo_label.config({'text': user_entry.get()})
user_entry = Entry(root, font=('aria label', 15), fg='black')
user_entry.insert(0, 'Enter your text here')
user_entry.bind("<KeyRelease>", user_entry_changed)
user_entry.pack()
echo_label = Label(root, text='<Will echo here>')
echo_label.pack()
root.mainloop()
Output: Here is the resulting output after entering 'abc' in the entry field:
from tkinter import *
import tkinter.ttk as ttk
root = Tk()
root.title("TEST ")
lab_pro_val4=Label(root)
lab_pro_val4=Label(root, text="Anything input", width=30, height=1 )
lab_pro_val4.pack()
ent_pro_val4 = Entry(root)
ent_pro_val4.pack()
def btncmd4_add():
tru = (ent_pro_val4=='TEST123')
print(tru)
btn4_ppid = Button(root, text="Check ", command= btncmd4_add,bg = "white")
btn4_ppid.pack()
root.mainloop()
I used Tkinter but I had some trouble.
My question : why is it diff from entry to str(same)
I type the 'TEST123' in entry box.
But it's False... Why is it different?
Please let me know.
ent_pro_val4 is Entry widget, you need to get value of said widget, which can be done using .get(), that is replace
tru = (ent_pro_val4=='TEST123')
using
tru = (ent_pro_val4.get()=='TEST123')
image for that
I have few lines of code here which is login system which works fine but i can click on the Toplevel button multiple times when i provide the wrong password without closing the messagebox.How can i make it so that it has to be closed messagebox before i can make attempt again.
from tkinter import *
from tkinter import messagebox
def top():
if entry1.get() == "333":
log.destroy()
root.deiconify()
else:
messagebox.showerror("error", "try again")
root = Tk()
root.geometry("300x300")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()
You need to make the log window the parent of the dialog:
messagebox.showerror("error", "try again", parent=log)
By default it will use the root window (the Tk instance) as the parent which in this case is not what you want.
With hint from #furas this how to implement this:
create another function to the call it when the entry doesn't match and use grab_set method for the Toplevel window tp.grab_set().You can add your customarised image to the Toplevel window as well as message to display in the box(here: i use label to depict that)
from tkinter import *
from tkinter import messagebox
def dialog(): # this function to call when entry doesn't match
tp = Toplevel(log)
tp.geometry("300x100")
tp.title('error')
tp.grab_set() # to bring the focus to the window for you to close it
tp.resizable(width=False, height=False)
l = Label(tp, text="try again\n\n\n\n add your customarize image to the window")
l.pack()
def top():
if entry1.get() == "333":
log.destroy()
root.deiconify()
else:
dialog() # being called here
root = Tk()
root.geometry("300x300")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()
How do i increase or define window size of tkSimpleDialog box ?
import Tkinter, tkSimpleDialog
root = Tkinter.Tk()
root.withdraw()
test = tkSimpleDialog.askstring("testing", "Enter your search string text")
print test
Make the second parameter as big as you like:
import Tkinter, tkSimpleDialog
root = Tkinter.Tk()
root.withdraw()
test = tkSimpleDialog.askstring("testing", "Enter your search string text in the space provided")
print test
Starting from: http://effbot.org/tkinterbook/tkinter-dialog-windows.htm
I did the following:
import Tkinter as tk, tkSimpleDialog
class MyDialog(tkSimpleDialog.Dialog):
def body(self, master):
self.geometry("800x600")
tk.Label(master, text="Enter your search string text:").grid(row=0)
self.e1 = tk.Entry(master)
self.e1.grid(row=0, column=1)
return self.e1 # initial focus
def apply(self):
first = self.e1.get()
self.result = first
root = tk.Tk()
root.withdraw()
test = MyDialog(root, "testing")
print test.result
If you want geometry and the label's text to be customizable, you will probably need to override __init__ (the version given in the link should be a good starting point).
If you want to wider box you can make buttons wider from the source code or override it inside your code:
def buttonbox(self):
'''add standard button box.
override if you do not want the standard buttons
'''
box = Frame(self)
w = Button(box, text="OK", width=100, command=self.ok, default=ACTIVE)
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="Cancel", width=100, command=self.cancel)
w.pack(side=LEFT, padx=5, pady=5)
self.bind("<Return>", self.ok)
self.bind("<Escape>", self.cancel)
box.pack()
tkSimpleDialog doesn't allows you to change it's geometry
In place use Tk() only
Here you have an exmple where you'll see the difference between two windows
import Tkinter, tkSimpleDialog
root = Tkinter.Tk()
root.geometry('240x850+200+100')
#root.withdraw()
test = tkSimpleDialog.askstring("testing", "Enter your search string text")
root.mainloop()
print test