(python) Disable the button in Tkinter when passwords do not match - python

How to disable the button in tkinter window when two passwords does not match?
My work:
from tkinter import *
from functools import partial
root = Tk()
root.geometry('280x100')
root.title('Tkinter Password')
def validation_pw(ep,cp):
if ep.get() == cp.get():
Label(root, text = "Confirmed").grid(column=0, row=5)
else:
Label(root, text = "Not matched").grid(column=0, row=5)
# check_button['state'] = DISABLED <============================
ep = StringVar()
cp = StringVar()
Label1 = Label(root, text = "Enter Password").grid(column=0, row=0)
pwEnty = Entry(root, textvariable = ep, show = '*').grid(column=1, row=0)
# Confirmed password label
Label2 = Label(root, text = "Confirm Password").grid(column=0, row=1)
pwconfEnty = Entry(root, textvariable = cp, show = '*').grid(column=1, row=1)
validation_pw = partial(validation_pw, ep,cp)
check_button = Button(root, text = "check", command = validation_pw).grid(column=0, row=4)
root.mainloop()
It shows if two passwords are not matched.
Now, I want to disable the check button if two passwords are not matched. I want the user cannot try the passwords anymore if failure.
So in the function validation_pw, I add check_button['state'] = DISABLED.
However, an error pops out
TypeError: 'NoneType' object does not support item assignment
How to fix this issue? Thanks!

Your checkbutton is actually None, because it's the result of the grid function.
To fix it, first declare the button, next grid it.
Before:
check_button = Button([...]).grid(column=0, row=4) # Result of Button.grid function
print(check_button) # None
After:
check_button = Button([...])
check_button.grid(column=0, row=4)
print(check_button) # Button object ...

You get the error of NoneType because at one point it was assigned nothing. This is because you used .grid on the same line as the button.
Fixed code:
check_button = Button(root, text = "check", command = validation_pw)
check_button.grid(column=0, row=4)

Related

Tkinter - After Second Button Click, Change Button Function to Close Window

I am trying to figure out a way to change a button's text and functionality after I have clicked the Submit button a second time. In the below instance, I am trying to:
1) Change the button's text from Submit to Close after I have entered in the username/password fields for SecondName and have clicked Submit
2) Use the Close() function to close the window.
I have attempted to accomplish these two processes by using an if/else statement.
Tkinter Code
import tkinter as tk
root = tk.Tk()
user_var = tk.StringVar()
pass_var = tk.StringVar()
entries = {}
def Submit():
user = user_var.get()
passw = pass_var.get()
label_text = user_label["text"]
char = label_text.split()[0]
entries[char] = (user, passw)
if char == "FirstName":
user_label["text"] = "SecondName " + user_label["text"].split()[1]
pass_label["text"] = "SecondName " + pass_label["text"].split()[1]
user_var.set("")
pass_var.set("")
print(entries)
def Close():
root.quit()
user_label = tk.Label(root, text="FirstName Username", width=21)
user_entry = tk.Entry(root, textvariable=user_var)
pass_label = tk.Label(root, text="FirstName Password", width=21)
pass_entry = tk.Entry(root, textvariable=pass_var, show="•")
if user_entry["text"] == "SecondName":
sub_btn = tk.Button(root, text="Close", command=Close)
else:
sub_btn = tk.Button(root, text="Submit", command=Submit)
sub_btn.grid(row=2, column=0)
user_label.grid(row=0, column=0)
user_entry.grid(row=0, column=1)
pass_label.grid(row=1, column=0)
pass_entry.grid(row=1, column=1)
root.mainloop()
Current Result
Expected Result
The main problem here is the misunderstanding of how event driven programming works. The following line of code runs ONLY when the tkinter window is initially drawn.
if user_entry["text"] == "SecondName":
sub_btn = tk.Button(root, text="Close", command=Close)
else:
sub_btn = tk.Button(root, text="Submit", command=Submit)
Which means user_entry["text"] is never "SecondName". Furthermore, user_entry["text"] does not do what you expect it to be doing, it returns the name of the textvariable option and not the contents of the entry widget, what you need to do is change your function to use elif:
def Submit():
user = user_var.get()
passw = pass_var.get()
label_text = user_label["text"]
char = label_text.split()[0]
entries[char] = (user, passw)
if char == "FirstName":
user_label["text"] = "SecondName " + user_label["text"].split()[1]
pass_label["text"] = "SecondName " + pass_label["text"].split()[1]
elif char == "SecondName":
sub_btn.config(text='Close', command=Close) # Change button if `char` is "SecondName" only
user_var.set("")
pass_var.set("")
print(entries)
Side Note: To get the value inside the entry widget, you can use user_entry.get() or user_var.get()

Extract text from Entry Box ktinker Python

I have a tkinter project and need the text, I put it in the Entry box, printed it in the console. but when I use get() I get empty space in the console.
this is my interface. when I put 1234 I could place it above when I click the button "Pull record data" but I need that number in my console. to use that entry for other things. but when I try to print it I get nothing in the console like this:
root = tk.Tk()
root.geometry("390x500")
root.title("Airtable Project Tracker")
str = tk.StringVar(root)
str.set('ie. recveGd8w9ukxLkk9')
tk.Label(root, textvariable=str).pack()
recid = tk.Entry(root, width=50)
recid.pack(pady = 10)
print(recid.get())
id_button = tk.Button(root, text="Pull record data", width=50, command = lambda:str.set(recid.get()))
id_button.pack(pady = 5)
As you have already implemented it above, you can get the input plane value using get() (recide.get()). Another method is to use the parameter textvariable as shown below. Where the value can be accessed again using get() (text.get()).
text = tk.StringVar()
recide = tk.Entry(root, width=50, textvariable=text)
In my test case I could not reproduce this error. I modified their code slightly to output the value in the console as shown below.
import tkinter as tk
def test():
str.set(recid.get())
print(recid.get())
root = tk.Tk()
str = tk.StringVar(root)
str.set('ie. recveGd8w9ukxLkk9')
tk.Label(root, textvariable=str).pack()
recid = tk.Entry(root, width=50)
recid.pack(pady = 10)
print(recid.get())
id_button = tk.Button(root, text="Pull record data", width=50, command = test)
id_button.pack(pady = 5)
root.mainloop()

Checkbutton does not work on a nested tkinter window

I am trying to create a Tkinter window with a button which when clicked will provide with a new window. The new window has a checkbox and I want some actions to be done based on the checkbox value.
from tkinter import *
from tkinter import messagebox
def my_command():
def run():
pass
def cb_command():
f1 = fname1.get()
messagebox.showinfo("First Name", f1)
if cbVar.get() == 1:
messagebox.showinfo(cbVar.get())
my_button['state'] = 'active'
else:
messagebox.showinfo("Not found!")
my_button['state'] = 'disabled'
root = Tk()
root.geometry("200x200")
fname = Label(root, text="First Name")
fname.grid(row= 0, column = 0, sticky = "news", padx=5, pady=5)
fname1 = Entry(root, width = 10)
fname1.grid(row =0, column = 1, sticky = "news", padx=5, pady=5)
cbVar = IntVar()
cb1 = Checkbutton(root, text="Please check this", variable=cbVar, onvalue=1, offvalue=0, command=cb_command)
cb1.grid(row = 1, column = 0)
my_button = Button(root, text = "Run", bg = '#333333', fg='#ffffff', font = 'Helvetica', command = run, state='disable')
my_button.grid(row = 2, column = 0)
root.mainloop()
window = Tk()
window.geometry("200x200")
button1 = Button(window, text = "Run", command = my_command)
button1.pack()
window.mainloop()
I wrote this simple code which works fine with all other entry widgets. However, the checkbutton in the new window does not work. Can someone suggest any alternative?
Update:
Sorry, that I didn't clarify what actions to be done. I want the checkbox when clicked impact the state of the "Run" button in the toplevel window. The actual actions are based on the "Run" button.
Thank you Thingamabobs for suggesting a very simple solution. Just replaced one instance of Tk with Toplevel and it works.
from tkinter import *
def new_window():
second_window = Toplevel()
def checkbutton_checked():
# If you just want to take some action, once the checkbutton has been checked, you could do this here
# Alternatively you could also add a button to the toplevel and on click check the value of
# the checkbutton and perform actions based on that...
cb1.configure(text="Checkbutton checked")
cb1 = Checkbutton(second_window, text="Check here", command=checkbutton_checked)
cb1.pack()
window = Tk()
b1 = Button(window, text="Open new window", command=new_window)
b1.pack()
window.mainloop()
I hope this provides some help and you can solve your problem, if not let me know please.
Further details about the purpose of the checkbutton would also help me.

One of my variables is being printed but the other is not in tkinter Entry boxes

I'm trying to create a function in tkinter where I can print out what the user writes in a Entry box. I'm able to print out ask_an_entry_get, but when I try to print what_is_answer_entry_get
, I get nothing my empty spaces.
Please find out the problem here. Also I'm using the Entry widget, along with the get() function, to get input from the user.
def answer_quizmaker_score():
print(ask_an_entry_get)
print(what_is_answer_entry_get)
I made a lot of global variables so I could use them all around my code.
global what_is_answer_entry
what_is_answer_entry = Entry(root4)
what_is_answer_entry.pack()
I then used the get() function to retrieve what the user typed.
global what_is_answer_entry_get
what_is_answer_entry_get = what_is_answer_entry.get()
This is the exact process I did for both ask_an_entry_get and what_is_answer_entry_get. However for some reason only ask_an_entry_get is printed, while what_is_answer_entry_get is printing nothing in the console.
from tkinter import *
root = Tk()
root.geometry("500x500")
txt1 = StringVar()
txt2 = StringVar()
def txt_printer():
print(txt1.get())
print(txt2.get())
x = Entry(root, textvariable=txt1, width=20)
x.place(x=0, y=0)
y = Entry(root, textvariable=txt2, width=20)
y.place(x=0, y=50)
btn_print = Button(root, text="print", command=txt_printer)
btn_print.place(x=0, y=100)
# Or if you want to show the txt on window then:
def txt_on_window():
lb1 = Label(root, text=txt1.get())
lb1.place(x=0, y=200)
lb2 = Label(root, text=txt2.get())
lb2.place(x=0, y=235)
btn_print_on_window = Button(root, text="print on screen", command=txt_on_window)
btn_print_on_window.place(x=0, y=150)
root.mainloop()

tkinter TextVar.get() returns '.!entry' not a string

I have the below code which for some reason when i call .get() to the textvariable on tkinter.Entry, I get '.!entry' instead of the string I am expecting. How can I fix this?
def getter():
final = e1str_var.get()
e1str_var = StringVar()
e1 = Entry(root, textvar=e1str_var)
e1.grid(row=4, column=0)
print(getter())
Returns '.!entry'
Your question seems unclear, you will be able to use get() of an Entry field on a certain action like a button click.
In the below code I made a button as well and when you click this button you will get what is written inside the textbox as your output.
from tkinter import *
def helloCallBack():
print(E1.get())
top = Tk()
L1 = Label(top, text="User Name")
L2 = Button(top, text="Click",command = helloCallBack)
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
L2.pack(side = BOTTOM)
print(E1.get())
top.mainloop()
You can only get the string in a text box when perfomed some event like button click or binding label or some other widget with an event. Here I have used a button for using get().
from tkinter import *
root=Tk()
def getter():
final = e1str_var.get()
print(final)
e1str_var = StringVar()
e1 = Entry(root, textvar=e1str_var)
b1=Button(root,text="Click",command=getter)
e1.grid(row=4, column=0)
b1.grid(row=5,column=0)
root.mainloop()

Categories

Resources