如何让一个 tkinter 框显示在另一个 tkinter window 中?

How do I get a tkinter box to show up in another tkinter window?

我正在尝试为密码存储和散列系统制作 GUI,但我遇到了障碍。我有 2 个按钮,一个用于登录,一个用于创建帐户。单击登录按钮时,将打开一个新的 tkinter window 和登录页面。但是,登录按钮应该显示在第二页上,但实际上没有,我也不知道为什么。这是完整系统的代码:

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()

这里是第二个 window 的代码。出于某种原因,我还必须导入 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()

感谢您的帮助!

这里。我完全修改了您的代码,并且它有效(据我所知)。

我完全删除了 textvariableStringVar()。不需要它们。您的代码无法正常工作的原因是您检查了文件名 "string",但您需要添加“.txt”以获得完美的相等性。

代码:

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()

正如@stovfl 提到的,我还冒昧地添加了 Toplevel() 个实例,而不是多次使用 Tk()

希望对您有所帮助!