Reputation: 40
I am trying to build a login window and after logging in, a second window with a list of options appears, the user should select a number and submit it in the second window. The login form works well, but the problem is that I am not able to extract the entered value from second window to put it in the function Admin() that is responsible for printing. I hope I am clear, if not I will answer any comments, thank you.
from tkinter import *
#User experience
def Admin():
global selection
value = selection.get()
print("hi",selection.get())
if value == '1':
print("1")
elif value == '2':
print("2")
elif value == '3':
print("3")
elif value == '4':
print("4")
elif value == '5':
print("5")
elif value == '6':
print("6")
elif value == '7':
print("7")
import time
incorrect = 0
def login():
#skipping the if username and password matches
AdminF()
#defining loginform function
def Loginform():
global login_screen
login_screen = Tk()
#Setting title of screen
login_screen.title("Login Form")
#setting height and width of screen
login_screen.geometry("300x250")
#declaring variable
global message
global username
global password
username = StringVar()
password = StringVar()
message=StringVar()
#Creating layout of login form
Label(login_screen,width="300", text="Please enter details below", bg="orange",fg="white").pack()
#Username Label
Label(login_screen, text="Username * ").place(x=20,y=40)
#Username textbox
Entry(login_screen, textvariable=username).place(x=90,y=42)
#Password Label
Label(login_screen, text="Password * ").place(x=20,y=80)
#Password textbox
Entry(login_screen, textvariable=password ,show="*").place(x=90,y=82)
#Label for displaying login status[success/failed]
#Login button
Button(login_screen, text="Login", width=10, height=1, bg="orange",command=login).place(x=105,y=130)
Label(login_screen, text="" ,textvariable=message).place(x=95,y=100)
login_screen.mainloop()
class AdminF:
def __init__(self):
global selection
selection = StringVar()
display = Tk()
display.title("ADMIN")
display.geometry("300x250")
label = Label(display,width="300", text ="Welcome Admin", bg="orange",fg="white")
label.pack()
label = Label(display,width="300", text ="Please Select a Number", bg="white",fg="black")
label.pack()
label = Label(display,width="300", text ="\n1.print 1\n\
2.print 2 \n\
3.print 3\n\
4.print 4 \n\
5.print 5\n\
6.print 6 \n\
7.print 7 ", bg="orange",fg="white")
label.pack()
# Label(self, text="Selection: ").place(x=20,y=180)
Entry(display, textvariable=selection).place(x=20,y=180, width = "260")
#selection = entry.get()
Button(display, text="submit", width=10, height=1, bg="orange", command = Admin).place(x=105,y=210)
print("\n",selection.get())
display.mainloop
Loginform()
Upvotes: 0
Views: 68
Reputation:
Another way of expressing Don't use two instances of Tk, use Toplevel instead. – by Thingamabobs is stating:
Change the way of creating a second window from
display = Tk()
to
display = Toplevel(login_screen)
and you will get the value from the second window.
With
display = Toplevel(login_screen)
display.grab_set()
you can in addition also prevent interactions with the login_screen
window and this way also trouble caused by creating multiple ADMIN windows from the login which all react to the submit button, but only one delivers the right user input value.
In the picture above clicking the right submit button will give the value 4 and not as eventually expected 7 .
Maybe someone with deeper insight into tkinter can point out in the comments how it comes that the Button press event is properly processed in the additional created windows but changes to the Entry content aren't ?
See https://www.pythontutorial.net/tkinter/tkinter-toplevel/ for more details about creating multiple windows in tkinter.
By the way: the
display.mainloop
command can be removed from your code in order to avoid confusion if trying to guess what it does and why it is necessary.
Upvotes: 1