Using Tkinter to disable entry with specified input - python-3.x

I would like to use Tkinter to be able to disable one entry if 'no' is selected from a drop down menu.
from tkinter import *
def disableEntry(entry):
entry.config(state='disable')
def allowEntry(entry):
entry.config(state='normal')
def main():
print("test")
root = Tk() #create a TK root window
root.title("Lunch and Learn") #Title of the window
L1 = Label(root, text = "Label 1").grid(row=0, column=0, padx=30, pady=(20,5))
L2 = Label(root, text = "Label 2").grid(row=1, column=0, pady=5)
var = StringVar()
E1 = Entry(root,bd =3)
E1.grid(row=0, column=1)
D1 = OptionMenu(root,var,"yes","no")
D1.grid(row=1,column=1)
if var.get() == 'no':
disableEntry(E1)
elif var.get() == 'yes':
allowEntry(E1)
B2 = Button(text = "Submit", command=main).grid(row=4, column=2)
root.mainloop()
the above code is a simple example of what i have tried. I have created two functions called 'disableEntry' and 'allowEntry' which should change the state of the entry box but they don't appear to do anything when i change the input of the drop down menu.
i dont know if i am approaching this the wrong way or if there is a standardized way to do this.
any help would be appreciated.

You need a way to check the state of the selection after it is changed. That can be achieved with adding a callback command to the OptionMenu widget.
You were checking the correct variable, but the point you were checking it at was before the screen window had even displayed.
from tkinter import Label, StringVar, OptionMenu, Entry, Tk, Button
# change the state of the Entry widget
def change_state(state='normal'):
E1.config(state=state)
def main():
print("test")
# callback function triggered by selecting from OptionMenu widget
def callback(*args):
if var.get() == 'no':
change_state(state='disable')
elif var.get() == 'yes':
change_state(state='normal')
root = Tk() #create a TK root window
root.title("Lunch and Learn") #Title of the window
L1 = Label(root, text="Label 1").grid(row=0, column=0, padx=30, pady=(20, 5))
L2 = Label(root, text="Label 2").grid(row=1, column=0, pady=5)
var = StringVar()
E1 = Entry(root, bd=3)
E1.grid(row=0, column=1)
D1 = OptionMenu(root, var, "yes", "no", command=callback)
D1.grid(row=1, column=1)
B2 = Button(text = "Submit", command=main).grid(row=4, column=2)
root.mainloop()

Related

name 'main_window' is not defined

from tkinter import *
def create_main_window():
global main_window
main_window = Toplevel()
main_window.update()
entrance_window = Tk()
first_text_label = Label(entrance_window, text="you are in:").grid(row=0, column=0)
place_entry = Entry(entrance_window).grid(row=0, column=1)
submit_button = Button(entrance_window, text="Submit", command=create_main_window).grid(row=1, column=0, columnspan=2)
Label(main_window, text=f"{place_entry}").pack()
entrance_window.mainloop()
the program should open a new window with the text from the entry box from the first window but it either shows None if I write
Label(main_window, text=f"{place_entry}").pack()
in the create_main_window or it gives me an error saying that main_window is not defined if I write it after the button code.
Can someone help with this?
Try this:
from tkinter import *
def create_main_window():
global main_window
main_window = Toplevel(main_window)
label = Label(main_window, text=f"{place_entry.get()}")
label.pack()
# main_window.update() # This is useless
entrance_window = Tk()
first_text_label = Label(entrance_window, text="You are in:")
first_text_label.grid(row=0, column=0)
place_entry = Entry(entrance_window)
place_entry.grid(row=0, column=1)
submit_button = Button(entrance_window, text="Submit", command=create_main_window)
submit_button.grid(row=1, column=0, columnspan=2)
entrance_window.mainloop()
I moved the label creation inside create_main_window. Also please note that using var = a().b(), saves what ever b() returns inside var. That is why when you use var = Entry(...).pack(...), var is always None.
This is because you are trying to add a Label to an object that doesn't exist. Move the Label function to the create_main_window() function, like below:
from tkinter import *
def create_main_window():
global main_window, entrance_window
main_window = Toplevel()
place_entry = Entry(entrance_window).grid(row=0, column=1)
Label(main_window, text=f"{place_entry}").pack()
main_window.update()
entrance_window = Tk()
first_text_label = Label(entrance_window, text="you are in:").grid(row=0, column=0)
submit_button = Button(entrance_window, text="Submit", command=create_main_window).grid(row=1, column=0, columnspan=2)
entrance_window.mainloop()

Show tkinter widget when checkbox is checked

I'm trying to make the entry widget show once the checkbutton is checked and hide when it's not.
from tkinter import *
master = Tk()
master.geometry('500x400')
other = IntVar()
Checkbutton(master, text="Other", variable=other, command=toggle()).grid(row=10, sticky=W)
def toggle():
if other.get()==1:
Entry(master,width=50).grid(row=11, sticky=W)
else:
Entry(master,width=50).grid_remove()
Try this:
from tkinter import *
def toggle():
if other.get():
ent.grid(row=11, sticky=W)
else:
ent.grid_forget()
master = Tk()
master.geometry('500x400')
other = BooleanVar()
Checkbutton(master, text="Other", variable=other, command=toggle).grid(row=10, sticky=W)
ent=Entry(master,width=50)
master.mainloop()

Python Tkinter Button not appearing with grid

I'm trying to program a little calculator in python3 with Tkinter. When I try to organize my buttons and the input filed with grid(), my buttons are not appearing. I am sure I forgot something but hopefully someone can find my issue.
from tkinter import *
gleichung = ""
def zahl_gedrückt(num):
global gleichung
gleichung = gleichung + str(num)
gleichung_text.set(gleichung)
if __name__ == "__main__":
app = Tk()
app.title("Taschenrechner")
app.geometry("400x600")
gleichung_text = StringVar()
gleichung_text.set('Gleichung...')
gleichung_textbox = Entry(app, font=("Calibri 25"), state='disabled', width=400, justify='center', textvariable=gleichung_text)
gleichung_textbox.grid(row=0, columnspan=4)
button1 = Button(app, text=' 1 ', font=("Calibri 40"), command=lambda: zahl_gedrückt(1)).grid(row=2, column=0)
button2 = Button(app, text=' 2 ', font=("Calibri 40"), command=lambda: zahl_gedrückt(2)).grid(row=2, column=1)
app.mainloop()

I am having trouble making an input text box that goes through a function and outputs test to an output textbox with Tkinter

im new to Python3.6.2
So I want a program that takes an input (Via text box from tkinter) and outputs a word in my custom "language"
with this function
def Mescre(n):
Words = (n)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
print(Words.translate(Mes))
and here's what i want the window to look like
from tkinter import*
root = Tk()
Mescre = Label(root, text="Input:")
English = Label(root , text="Output:")
label1.grid(row=0, sticky=E)
label2.grid(row=1, sticky=E)
entry1 = Entry(root)
entry2 = Entry(root)
entry1.grid = (row=0, column=1)
entry2.grid = (row=1, column=1)
root.mainloop()
if "hello" was in the Input text box, i want the output to be "coqqu" in the Output text box.
See my example below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.sv = StringVar()
self.Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
self.entry = Entry(self.root, textvariable = self.sv)
self.label = Label(self.root)
self.entry.pack()
self.label.pack()
self.sv.trace("w", self.callback)
def callback(self, *args):
self.label.configure({"text": self.entry.get().translate(self.Mes)})
root = Tk()
App(root)
root.mainloop()
Here we define a StringVar() to be the value of the attribute textvariable for the Entry widget.
We then assign a callback to a trace() on the variable so that whenever the variable is updated (When someone types in the Entry) we call callback().
Within callback() we use configure() on the Label widget in order to set the text to equal the post translation version of the value of the Entry widget.
This creates a "live updating" translation effect.
Here's a basic example:
import tkinter as tk
root = tk.Tk()
def Mescre():
val = textfield.get()
Words = (val)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
print(Words.translate(Mes))
textfield = tk.Entry(root)
textfield.pack()
button = tk.Button(root, command=Mescre, text='Push')
button.pack()
root.mainloop()
Updated:
import tkinter as tk
root = tk.Tk()
def Mescre():
val = textfield.get()
Words = (val)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
translation = Words.translate(Mes)
#print(translation)
outputfield.delete(0, tk.END)
outputfield.insert(0, translation)
textfield = tk.Entry(root)
textfield.pack()
outputfield = tk.Entry(root)
outputfield.pack()
button = tk.Button(root, command=Mescre, text='Push')
button.pack()
root.mainloop()

How can i edit this code i've made so far so that when i click the button 'signup' it closes that gui and opens the next one

i am using tkinter to make a gui and have made various different buttons and now i have made all this i am unsure how to correctly make the first gui box close as the second one opens (sign_in function)
from tkinter import *
class login:
def __init__(self, master):
frame = Frame(master)
frame.grid()
self.button1 = Button(frame, text="signup", fg="green",command=self.sign_in)
self.button2 = Button(frame, text="sign in", fg="black",)
self.button3 = Button(frame, text="quit", fg="red", command=frame.master.destroy)
self.button1.grid(stick=W)
self.button2.grid(stick=W)
self.button3.grid(stick=W)
def sign_in(self):
frame = Frame()
frame.grid()
name = Label(root, text="Name: ")
password = Label(root, text="password: ")
entry1 = Entry(root)
entry2 = Entry(root)
name.grid(row=0, sticky=E)
password.grid(row=1, sticky=E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
c = Checkbutton(root, text="keep me logged in")
c.grid(columnspan=2, sticky="w")
root = Tk()
account=login(root)
root.mainloop()
Your code contains some indentation errors so I'll just go by your question.
when i click the button 'signup' it closes that gui and opens the next one
You can do so by first withdrawing your root window like this: root.withdraw() which will hide your original window. Then create a Toplevel window like this: newWindow = tk.Toplevel(root) to create a new window. You will just need to place these lines in the button command call.
Here's what you can change in the sign_in note that I changed all the masters to frame and not root:
def sign_in(self):
root.withdraw()
frame = Toplevel(root)
name = Label(frame, text="Name: ")
password = Label(frame, text="password: ")
entry1 = Entry(frame)
entry2 = Entry(frame)
name.grid(row=0, sticky=E)
password.grid(row=1, sticky=E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
c = Checkbutton(frame, text="keep me logged in")
c.grid(columnspan=2, sticky="w")

Resources