无法在 tkinter 中使用 .get 函数

问题描述 投票:0回答:0

我正在尝试在 tkinter 中创建 3 个窗口:

  • 登录窗口
  • 注册窗口
  • 一个空窗口(后面会用到)

应用程序从登录窗口开始,并在用户决定首先注册时关闭此窗口。当用户决定登录时,登录窗口也会关闭。

我认为我在正确的轨道上,但是,我坚持打印出用于注册的电子邮件地址字段的值。这听起来很傻,但有些东西我没有看到。到目前为止,这是我的代码:

from tkinter import *

loginWindow = Tk()

loginWindow.title("Log in window")
loginWindow.minsize(width=250, height=250)

loginEmailAddress = StringVar()
registrationEmailAddress = StringVar()
checkEmailAddress = StringVar()
password = StringVar()

def createRegistrationWindow():
    closeLoginWindow()
    registrationWindow = Tk()
    registrationWindow.title("Register")
    registrationWindow.minsize(width=250, height=250)

    Label(registrationWindow, text="Email address").grid(column=0, row=0)
    Entry(registrationWindow, bd=5, textvariable=registrationEmailAddress).grid(
        column=1, row=0)

    Label(registrationWindow, text="Once more please").grid(column=0, row=1)
    Entry(registrationWindow, bd=5, textvariable=checkEmailAddress).grid(
        column=1, row=1)

    Label(registrationWindow, text="Password").grid(column=0, row=2)
    Entry(registrationWindow, show="*", bd=5, textvariable=password).grid(column=1, row=2)
    Button(registrationWindow, text="Cancel", command=registrationWindow.destroy).grid(
        column=1, row=3)

    Button(registrationWindow, text="Register window", command=checkEmailAddress).grid(column=0,row=3)

def checkEmailAddress():
    #I'm planning on using this function to check if the email adresses are equal
    print(registrationEmailAddress.get())

def closeLoginWindow():
    loginWindow.destroy()

def logIn():
    formAfterLoggingIn = Tk()
    formAfterLoggingIn.title("Empty form")
    formAfterLoggingIn.minsize(width=250, height=250)
    closeLoginWindow()

labelEmailAddress = Label(loginWindow, text="Email address").grid(column=0, row=0)
entryEmailAddress = Entry(loginWindow, bd=5, textvariable=loginEmailAddress).grid(column=1, row=0)

labelPassword = Label(loginWindow, text="Password").grid(column=0, row=1)
entryPassword = Entry(loginWindow, show="*", bd=5, textvariable=password).grid(column=1, row=1)

registrationButtonOnLoginWindow = Button(loginWindow, text="Register",
                                         command=createRegistrationWindow).grid(column=0, row=2)
cancelButtonOnLoginWindow = Button(loginWindow, text="Cancel", command=closeLoginWindow).grid(
    column=1, row=2)
loginButtonOnLoginWindow = Button(loginWindow, text="Log in", command=logIn).grid(column=2,
                                                                                                         row=2)

loginWindow.mainloop()

我试过 .get() 电子邮件地址的值,我试过简单地打印出电子邮件地址(把它最终作为 PY_VAR0 而不是值),我玩过全局变量和将变量移到某些函数之外,但每次更改都会引入自己的新问题。

我不知道,但也许我只是没有看到 tkinter 背后的逻辑。

variables tkinter printing scope get
© www.soinside.com 2019 - 2024. All rights reserved.