我是python的初学者,我正在为自己做点小事,但是在单击按钮并打开新窗口后无法弄清楚如何破坏第一个窗口,它将保留变量在第一个窗口中输入第二个窗口,对吗?
from tkinter import *
import tkinter as tk
import sys
def showdata():
email1 = email.get()
password2 = password1.get()
print(email1 +" "+ password2)
exec(open("scratch.py").read())
window = tk.Tk()
window.title("CANmail")
window.geometry("300x150")
em = Label(window, text="E-mail").place(relx=0.2,rely=0.2)
pd = Label(window, text="Password").place(relx=0.2,rely=0.4)
email = Entry(window)
password1 = Entry(window)
email.grid(row=0, column=1)
email.place(relx=0.5,rely=0.2)
password1.grid(row=1, column=1)
password1.place(relx=0.5,rely=0.4)
btn = Button(window, text="Log In", command=showdata)
btn.grid(row=2, column=1)
btn.place(relx=0.5,rely=0.7,anchor=CENTER)
window.mainloop()
单击按钮并调用显示数据功能时调用功能:
def quit(window):
window.destroy()
它认为它将为您提供帮助。这是另一个例子:
import tkinter
class App():
def __init__(self):
self.root = Tkinter.Tk()
button = Tkinter.Button(self.root, text = 'root quit', command=self.quit)
button.pack()
self.root.mainloop()
def quit(self):
self.root.destroy()
app = App()
我通过练习发现了如何通过仅将window.destroy()
命令添加到另一个文件的开头来执行此操作,因为它需要在第一个文件的循环之后写入]
您可以从第一个窗口保存变量,而仅在另一个窗口中使用它们。
为什么不像这样从头开始使用import?并以这种方式将变量传递到那里:
scratch.py文件:
def customize_window(customer_email,customer_password):
w = Tk()
w.geometry('300x150')
l1 = Label(w,text = 'Your Email is:{0}'.format(customer_email))
l2 = Label(w,text = 'Your Password is:{0}'.format(customer_password))
l1.pack()
l2.pack()
w.mainloop()
然后转到您的主文件并执行此操作:
from .scratch import customize_window
##your code...
...
...
root.destroy()
customize_window(customer_email = email.get(), customer_password = password1.get())
您可以在登录窗口中使用Frame
,并在成功登录后将其隐藏(在showdata
函数中),然后在主窗口中显示另一个,如果您使用的是类,则无需传递变量,使用self.
可访问它们
尝试以下示例:
from tkinter import Tk, Frame, Label, Entry, Button, Toplevel
class MyApp(object):
""""""
def __init__(self, parent):
self.root = parent
self.root.title("Main frame")
self.frame = Frame(parent)
self.frame.pack()
Label(self.frame).grid(row=0,column=0) # Add a space between top border and E-mail widget
self.em = Label(self.frame, text="E-mail")
self.em.grid(row=1,column=0)
self.pd = Label(self.frame, text="Password")
self.pd.grid(row=2,column=0)
self.email = Entry(self.frame)
self.password1 = Entry(self.frame)
self.email.grid(row=1, column=1)
self.password1.grid(row=2, column=1)
self.btn = Button(self.frame, text="Log In", command=self.showdata)
self.btn.grid(row=3, column=1)
def hide(self):
self.root.withdraw()
def openFrame(self):
self.hide()
otherFrame = Toplevel()
otherFrame.geometry("400x300")
otherFrame.title("otherFrame")
handler = lambda: self.onCloseOtherFrame(otherFrame)
btn = Button(otherFrame, text="Close", command=handler)
btn.pack()
def onCloseOtherFrame(self, otherFrame):
otherFrame.destroy()
self.show()
def show(self):
self.root.update()
self.root.deiconify()
def showdata(self):
email1 = self.email.get()
password2 = self.password1.get()
print(email1 +" "+ password2)
# exec(open("scratch.py").read())
self.openFrame()
if __name__ == "__main__":
root = Tk()
root.geometry("300x100")
app = MyApp(root)
root.mainloop()