Python Tkinter 窗口关闭问题



此脚本将打开第一个登录页面。如果我输入正确的用户名和密码,则应打开另一个窗口并关闭登录窗口。 我的问题是,如果我关闭第一个窗口,就会出现第二个窗口。我怎样才能克服这个问题?我需要当我关闭第一个窗口时,脚本应该终止。

##libraries
from tkinter import *
import paramiko
import time
from tkinter.ttk import Combobox

global win
LARGE_FONT= ("Verdana", 12)
NORM_FONT = ("Helvetica", 10)
SMALL_FONT = ("Helvetica", 8)
## def to check username and password then start the next loop
def done():
global username1
global password1
if webui_username_Entry.get() == "alcatel" and webui_password_Entry.get() == "Nokia@2016" :
root.quit()
username1 = webui_username_Entry.get()
password1 = webui_password_Entry.get()
root.destroy()
else:
popupmsg("Wrong Username or password")
import tkinter as tk
from tkinter import ttk
def popupmsg(msg):
popup = tk.Tk()
popup.wm_title("Error")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Try Again", command = popup.destroy)
B1.pack()
popup.mainloop()

class MyWindow():
global root
import tkinter as tk
root = tk.Tk()
image = r"D:PythonMy ProjectsNokia1.png"
icon = PhotoImage(file=image)
root.call('wm', 'iconphoto', root._w, icon)

canvas1 = tk.Canvas(root, width=400, height=300)
canvas1.pack()
global webui_username_Entry
global webui_password_Entry
image = tk.PhotoImage(file=r"D:PythonMy ProjectsNokia.png")  # Use self.image
canvas1.create_image(50, 10, image=image, anchor=tk.NW)
webui_username_Entry = tk.Entry(root)
canvas1.create_window(250, 150, window=webui_username_Entry)
webui_username = tk.Label(root, text="Enter Username")
canvas1.create_window(125, 150, window=webui_username)
webui_password_Entry = tk.Entry(root,show="*")
canvas1.create_window(250, 200, window=webui_password_Entry)
webui_password = tk.Label(root, text="Enter Password")
canvas1.create_window(125, 200, window=webui_password)
result = "result"
button1 = tk.Button(text='Login', command=done, bg='black', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 250, window=button1)
root.title('Nokia Login')
root.mainloop()
def __init__(self,win):
global cb
global profile_unknown
global profile_Voda
global profile_All
global outbox

self.new_username = Label(win, text='New Username')
self.new_password = Label(win, text='New Password')
self.profile = Label(win, text='Profile Domain')
self.result = Label(win, text='Result')
global webui_username
global webui_Password
webui_username = self.t1 = Entry(bd=3)
webui_Password = self.t2 = Entry()
outbox = self.outbox_E = Entry()
self.btn2 = Button(win, text='Create')
self.new_username.place(x=100, y=50)
self.t1.place(x=200, y=50)
self.new_password.place(x=100, y=100)
self.t2.place(x=200, y=100)
self.profile.place(x=100, y=150)
# self.b1=Button(win, text='Add', command=self.add)
self.b2 = Button(win, text='Create', command=auth)
self.b2.place(x=200, y=200)
# self.b2.bind('<Button-1>', self.sub)
# self.b1.place(x=100, y=150)
self.result.place(x=100, y=240)
self.outbox_E.place(x=200, y=240, width=200, height=40)
# Droplist
global data
global cb
profile_unknown = " unknown"
profile_Voda = " Voda"
profile_All = ' "Voda","unknown"'
data = ("Voda", "unknown", 'All Network')
cb = Combobox(window, values=data)
cb.place(x=200, y=150)
def auth():
## App Athuntication.
##inputs/variable
global ipa
ipa = "xx.xx.xx.xx"
# webui_user = str(input("Enter username                    :"))
# webui_pass = str(input("Enter password                    :"))
y = "yes"
n = "no"
global ssh
## here to try the first connection
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ipa, 22, 'root', 'Nokia@2016')
shell = ssh.invoke_shell()
print("Auth Success")

except paramiko.AuthenticationException:
print(" nWrong username or Password, Please try againn")
## while this error how please do the authi() function.
except  TimeoutError:
print("nIP is not reachable. please check connectionn")
## while this error show please do the authi() function.
if cb.get() == "unknown":
profile_d = profile_unknown
elif cb.get() == "Voda":
profile_d = profile_Voda
elif cb.get() == 'All Network':
profile_d = profile_All
window = Tk()
mywin = MyWindow(window)
image = r"D:PythonMy ProjectsNokia2.png"
icon = PhotoImage(file=image)
window.call('wm', 'iconphoto', window._w, icon)
window.title('Vendor tool')
window.geometry("500x400+10+10")
window.mainloop()

您的问题的解决方案很简单。你只需要给它添加逻辑

检查用户是否提供了正确的用户名或密码后,您可以使用Tk()类创建一个窗口:

windowName = Tk()

然后,将旧窗口的变量名称设为global您可以使用destroy方法将其关闭。

注意:仅当您尝试将创建新窗口的代码放入函数中时,才必须创建全局变量。如果你热衷于在 if 语句本身中实现代码,你不一定需要这样做。

最新更新