在Python3中如何关闭一个窗口同时打开另一个窗口

2024-04-20 12:14:30 发布

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

我对Tkinter非常陌生,必须将其用于我的编程课程,我正在寻找帮助,以了解如何在SuccessfulLogin窗口打开时销毁/隐藏登录窗口。记住我是新手。谢谢您。在

import tkinter
from tkinter import ttk

-----------------------------------------------------------------------------------------------------------------------在

^{pr2}$

-----------------------------------------------------------------------------------------------------------------------在

class successfulLogin():


def __init__(self, master):

    self.master = master
    self.master.title("Login Successful")
    self.master.geometry("700x700")
    self.master.configure(background="#fff1d0")

    self.confirmLabel = tkinter.Label(self.master, text = " ", font = ("Helvetica", 16), bg = "#fff1d0")
    self.confirmLabel.pack()

    self.quitButton = tkinter.ttk.Button(self.master, text = "Quit", command = self.quitWindow)
    self.quitButton.pack()

    self.logOutButton = tkinter.ttk.Button(self.master, text = "Log out", command = self.logout)
    self.logOutButton.pack()

def setName(self, username):
    name = username
    self.confirmLabel.configure(text = "welcome " + name)

def quitWindow(self):
    self.master.quit()

def logout(self):
    loginWindow(root)
    self.master.destroy()

-----------------------------------------------------------------------------------------------------------------------在

def centerWindow(width=300, height=200):
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))


root = tkinter.Tk()
centerWindow(310, 375)
root.resizable(False,False)
loginWindow(root)
root.mainloop(

Tags: textimportselfmastertkinterconfiguredefroot
1条回答
网友
1楼 · 发布于 2024-04-20 12:14:30

基于this answer可以生成下面的示例。在

import tkinter as tk

root = tk.Tk()

#In order to hide main window
root.withdraw()

tk.Label(root, text="This is the main window").pack()

aWindow = tk.Toplevel(root)

def change_window():
    #remove the other window entirely
    aWindow.destroy()

    #make root visible again
    root.iconify()
    root.deiconify()

tk.Button(aWindow, text="This is aWindow", command=change_window).pack()

root.mainloop()

在第一个示例中,有一个窗口aWindow,当您单击它上的按钮时,它会破坏aWindow,并使root窗口再次可见。在

相关问题 更多 >