簡體   English   中英

Python Tkinter window 關閉問題

[英]Python Tkinter window close issue

此腳本打開第一個登錄頁面。 如果我輸入正確的用戶名和密碼,另一個 window 應該打開並關閉登錄 window。 我的問題是,如果我關閉第一個 window,則會出現第二個 window。 我怎樣才能克服這個? 我需要當我關閉第一個 window 時,腳本應該終止。

##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:\Python\My Projects\Nokia1.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:\Python\My Projects\Nokia.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 again\n")
        ## while this error how please do the authi() function.

    except  TimeoutError:
        print("\nIP is not reachable. please check connection\n")
        ## 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:\Python\My Projects\Nokia2.png"
icon = PhotoImage(file=image)
window.call('wm', 'iconphoto', window._w, icon)
window.title('Vendor tool')
window.geometry("500x400+10+10")
window.mainloop()

您的問題的解決方案很簡單。 您只需要為其添加邏輯

檢查用戶是否提供了正確的用戶名或密碼后,您可以使用Tk() class 創建一個 window:

windowName = Tk()

然后將舊的 window 的變量名設為global ,您可以使用destroy方法將其關閉。

注意:僅當您嘗試將創建新 window 的代碼放入 function 時,才需要創建全局變量。 如果您對在 if 語句本身中實現代碼感興趣,則不必這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM