为什么If语句不能与tkinter一起使用

2024-04-26 18:31:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了两个py文件,一个包含登录GUI,另一个包含我称之为终端的所有函数和窗口

我正在努力做到以下几点: 在输入正确的凭证以打开/运行终端后,首先显示登录GUI窗口(第二个GUI窗口)

然而,无论我尝试了什么,我都没有在凭证之后打开第二个窗口。 我尝试过使用if语句,但是失败了,我唯一想做的就是在一个文件中重新编写整个代码,而在一个文件中包含这么多代码似乎不是一个好主意

登录GUI:

import tkinter as tk


# from tkinter_practice import *


def username(txt_username):
    if txt_username == "1":
        return True


def password(txt_password):
    if txt_password == "2":
        return True


def ok_button(gotten_username, gotten_password):
    if username(gotten_username) and password(gotten_password):
        print('login success')
        return True
    else:
        print('Incorrect Username or Password')
        return False


root2 = tk.Tk()
root2.title('Login window')
root2.geometry('400x200-550-400')
root2.columnconfigure(0, weight=1)
root2.columnconfigure(1, weight=1)
root2.columnconfigure(2, weight=1)
root2.rowconfigure(0, weight=1)
root2.rowconfigure(1, weight=1)
root2.rowconfigure(2, weight=1)

lbl_username = tk.Label(root2, text='Username')
lbl_username.grid(row=1, column=0)

ent_username = tk.Entry(root2)
ent_username.grid(row=1, column=1)

lbl_password = tk.Label(root2, text='Password')
lbl_password.grid(row=2, column=0, sticky='n')

ent_password = tk.Entry(root2)
ent_password.grid(row=2, column=1, sticky='n')


btn_ok = tk.Button(root2, text='LOGIN', command=lambda: ok_button(ent_username.get(), ent_password.get()), padx=10,
                   pady=5)
btn_ok.grid(row=3, column=1, sticky='nw', )

btn_cancel = tk.Button(root2, text='Cancel', command=quit, padx=10, pady=5)
btn_cancel.grid(row=3, column=1, sticky='n')

root2.mainloop()

代码的第二部分包含第二个窗口/GUI,我希望在第一个窗口的凭据为True后打开该窗口/GUI:

import tkinter as tk
from tkinter import *
# from PIL import ImageTk, Image
import webbrowser
import pytz
import datetime
import login_GUI
# from tkinter import messagebox

# Main frame/window

if login_GUI.ok_button(login_GUI.ent_username.get, login_GUI.ent_password.get):

    root = tk.Tk()
    root.title("Main terminal")
    root.geometry("800x600-50-50")
    # Here I get the icon for the left corner of bar on top
    root.iconbitmap(icon)
    new = 1
    url = "https://www.youtube.com"

    root.columnconfigure(0, weight=1)
    root.columnconfigure(1, weight=1)
    root.columnconfigure(2, weight=1)




    root.mainloop()

Tags: importiftkinterusernameguicolumnrootpassword
1条回答
网友
1楼 · 发布于 2024-04-26 18:31:14

您应该将主代码块放在函数的login_GUI.py内,如login(),然后在函数末尾返回凭证验证结果

登录\u GUI.py

import tkinter as tk

def login():
    validated = False   # credential validation result

    def username(txt_username):
        if txt_username == "1":
            return True

    def password(txt_password):
        if txt_password == "2":
            return True

    def ok_button(gotten_username, gotten_password):
        nonlocal validated
        if username(gotten_username) and password(gotten_password):
            print('login success')
            validated = True  # validation successful
            root2.destroy() # close the login window
        else:
            print('Incorrect Username or Password')

    root2 = tk.Tk()
    ...
    root2.mainloop()

    return validated

然后您可以使用login_GUI.login()如:

import login_GUI

if login_GUI.login():
    ...

相关问题 更多 >