[英]How do I get a tkinter box to show up in another tkinter window?
I'm trying to make a GUI for a password storage and hashing system, but I have hit a roadblock.我正在尝试为密码存储和散列系统制作 GUI,但遇到了障碍。 I am having 2 buttons, one to log in, and one to make an account.我有 2 个按钮,一个用于登录,一个用于创建帐户。 When the login button is clicked, a new tkinter window opens with the login page.单击登录按钮后,将打开一个新的 tkinter window 和登录页面。 however, the login button is supposed to show on the second page, but it doesn't, and I have no clue why.但是,登录按钮应该显示在第二页上,但它没有,我不知道为什么。 Here is the code for the full system:这是完整系统的代码:
import tkinter
from tkinter import*
username = ("Tom")
password = ("test")
usernameguess1 = ("")
passwordguess1 = ("")
def trylogin():
print ("Trying to login...")
if usernameguess.get() == username:
print ("Complete sucsessfull!")
messagebox.showinfo("Sucess ", "Successfully logged in.")
else:
print ("Error: (Incorrect value entered)")
messagebox.showinfo("Error", "Sorry, but your username or password is incorrect. Try again")
def loginpage():
#Gui Formatting
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1 Login")
window.geometry("300x150")
#Username and password boxes
usernametext = tkinter.Label(window, text="Username:")
usernameguess = tkinter.Entry(window)
passwordtext = tkinter.Label(window, text="Password:")
passwordguess = tkinter.Entry(window, show="*")
usernameguess1 = usernameguess
#login button
attemptlogin = tkinter.Button(text="Login", command=trylogin)
usernametext.pack()
usernameguess.pack()
passwordtext.pack()
passwordguess.pack()
attemptlogin.pack()
window.mainloop()
#Gui Formatting (again)
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1")
window.geometry("300x150")
loginbutton = tkinter.Button(text="Login", command=loginpage)
loginbutton.pack()
And here is the code for just the second window on its own.这是第二个 window 的代码。 For some reason I have to import tkinter message boxes aswell, or my IDLE errors out.出于某种原因,我还必须导入 tkinter 消息框,或者我的 IDLE 错误。
import tkinter
from tkinter import *
from tkinter import messagebox
username = ("Tom")
password = ("test")
usernameguess1 = ("")
passwordguess1 = ("")
def trylogin():
print ("Trying to login...")
if usernameguess.get() == username:
print ("Complete sucsessfull!")
messagebox.showinfo("Sucess ", "Successfully logged in.")
else:
print ("Error: (Incorrect value entered)")
messagebox.showinfo("Error", "Sorry, but your username or password is incorrect. Try again")
#Gui Formatting
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1 Login")
window.geometry("300x150")
#Username and password boxes
usernametext = tkinter.Label(window, text="Username:")
usernameguess = tkinter.Entry(window)
passwordtext = tkinter.Label(window, text="Password:")
passwordguess = tkinter.Entry(window, show="*")
usernameguess1 = usernameguess
#login button
attemptlogin = tkinter.Button(text="Login", command=trylogin)
usernametext.pack()
usernameguess.pack()
passwordtext.pack()
passwordguess.pack()
attemptlogin.pack()
window.mainloop()
Thanks for your help!谢谢你的帮助!
Here.这里。 I completely reworked your code, and it works(as far as I can tell).我完全修改了您的代码,并且它有效(据我所知)。
I completely removed the textvariable
and StringVar()
.我完全删除了textvariable
和StringVar()
。 They aren't needed.他们不需要。 The reason your code wasn't working is because you check the name of the file "string", but you need to add ".txt" in order to get a perfect equality.您的代码不起作用的原因是因为您检查了文件“string”的名称,但您需要添加“.txt”以获得完美的相等性。
Code:代码:
import tkinter
from tkinter import*
from tkinter import messagebox
username = ("Tom")
password = ("test")
usernameguess1 = ("")
passwordguess1 = ("")
def loginpage():
#Gui Formatting
root = tkinter.Toplevel()
#root.resizable(width=FALSE, height=FALSE)
root.title("HasherTest_V0.1 Login")
root.geometry("300x150")
#Username and password boxes
usernametext = tkinter.Label(root, text="Username:")
usernameguess = tkinter.Entry(root)
passwordtext = tkinter.Label(root, text="Password:")
passwordguess = tkinter.Entry(root, show="*")
usernameguess1 = usernameguess
def trylogin():
print ("Trying to login...")
if usernameguess.get() == username:
print ("Complete sucsessfull!")
messagebox.showinfo("Sucess ", "Successfully logged in.")
else:
print ("Error: (Incorrect value entered)")
messagebox.showinfo("Error", "Sorry, but your username or password is incorrect. Try again")
#login button
attemptlogin = tkinter.Button(root, text="Login", command=trylogin)
usernametext.pack()
usernameguess.pack()
passwordtext.pack()
passwordguess.pack()
attemptlogin.pack()
window.mainloop()
#Gui Formatting (again)
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1")
window.geometry("300x150")
loginbutton = tkinter.Button(window, text="Login", command=loginpage)
loginbutton.pack()
window.mainloop()
I also took the liberty to, as @stovfl mentioned, add Toplevel()
instances instead of using Tk()
multiple times.正如@stovfl 提到的,我还冒昧地添加了Toplevel()
实例,而不是多次使用Tk()
。
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.