Python tkinter销毁窗口

时间:2019-10-27 07:19:37

标签: python python-3.x tkinter

我是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()

4 个答案:

答案 0 :(得分:0)

当您单击按钮并调用显示数据功能时,调用功能:

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()

答案 1 :(得分:0)

我通过实践发现了如何通过仅将package XXX; import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.HealthCheckHandler; import com.netflix.appinfo.InstanceInfo; import com.netflix.discovery.DiscoveryClient; import org.mockserver.integration.ClientAndServer; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.cloud.commons.util.InetUtils; import org.springframework.cloud.commons.util.InetUtilsProperties; import org.springframework.cloud.netflix.eureka.*; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.EventListener; import java.net.SocketException; @Configuration @ConditionalOnExpression("${workflow.engine.mock.enabled:false}") public class MockWorkflowEngineConfiguration { @Value("${workflow.engine.mock.application.name}") private String workflowEngineApplicationName; @Value("${workflow.engine.mock.application.port}") private Integer workflowEnginePort; @Autowired private EurekaInstanceConfigBean originalInstanceConfig; @Autowired private EurekaClientConfigBean originalClientConfig; @Autowired private ApplicationInfoManager applicationInfoManager; @Autowired private ApplicationContext applicationContext; @Autowired private ApplicationEventPublisher applicationEventPublisher; @Autowired private ObjectProvider<HealthCheckHandler> healthCheckHandler; @Autowired private EurekaServiceRegistry eurekaServiceRegistry; private EurekaRegistration workflowEngineEurekaRegistration; private DiscoveryClient workflowEngineDiscoveryClient; private ClientAndServer workflowEngineMockClient; @EventListener(ApplicationReadyEvent.class) public void initializeMockWorkflowEngine() throws SocketException{ workflowEngineDiscoveryClient = new CloudEurekaClient( createWorkflowEngineAppInfoManager(), duplicateEurekaClientConfig(), applicationEventPublisher); workflowEngineEurekaRegistration = EurekaRegistration.builder((CloudEurekaInstanceConfig) workflowEngineDiscoveryClient.getApplicationInfoManager().getEurekaInstanceConfig()) .with(workflowEngineDiscoveryClient) .with(workflowEngineDiscoveryClient.getApplicationInfoManager()) .with(healthCheckHandler).build(); eurekaServiceRegistry.register(workflowEngineEurekaRegistration); workflowEngineMockClient = new ClientAndServer(workflowEnginePort); workflowEngineMockClient.when( HttpRequest.request() .withMethod("GET") .withPath("/job") ) .respond( HttpResponse.response() .withStatusCode(200) .withBody("{ id: '1', name: 'default'}") ); } @EventListener(ContextClosedEvent.class) public void shutdownMockWorkflowEngine(){ workflowEngineDiscoveryClient.shutdown(); eurekaServiceRegistry.deregister(workflowEngineEurekaRegistration); workflowEngineMockClient.stop(true); } private ApplicationInfoManager createWorkflowEngineAppInfoManager() throws SocketException { EurekaInstanceConfigBean newInstanceConfig = new EurekaInstanceConfigBean(new InetUtils(new InetUtilsProperties())); newInstanceConfig.setEnvironment(applicationContext.getEnvironment()); newInstanceConfig.setAppname(workflowEngineApplicationName); newInstanceConfig.setInstanceId(applicationInfoManager.getInfo().getHostName() + ":" + workflowEngineApplicationName + ":" + workflowEnginePort); newInstanceConfig.setInitialStatus(InstanceInfo.InstanceStatus.UP); newInstanceConfig.setNonSecurePortEnabled(originalInstanceConfig.isNonSecurePortEnabled()); newInstanceConfig.setNonSecurePort(workflowEnginePort); newInstanceConfig.setHostname(applicationInfoManager.getInfo().getHostName()); newInstanceConfig.setSecurePortEnabled(originalInstanceConfig.isSecurePortEnabled()); newInstanceConfig.setSecurePort(originalInstanceConfig.getSecurePort()); newInstanceConfig.setDataCenterInfo(originalInstanceConfig.getDataCenterInfo()); newInstanceConfig.setHealthCheckUrl(originalInstanceConfig.getHealthCheckUrl()); newInstanceConfig.setSecureHealthCheckUrl(originalInstanceConfig.getSecureHealthCheckUrl()); newInstanceConfig.setHomePageUrl(originalInstanceConfig.getHomePageUrl()); newInstanceConfig.setStatusPageUrl(originalInstanceConfig.getStatusPageUrl()); newInstanceConfig.setStatusPageUrlPath(originalInstanceConfig.getStatusPageUrlPath()); newInstanceConfig.setIpAddress(originalInstanceConfig.getIpAddress()); newInstanceConfig.setPreferIpAddress(originalInstanceConfig.isPreferIpAddress()); ApplicationInfoManager manager = new ApplicationInfoManager(newInstanceConfig, (ApplicationInfoManager.OptionalArgs) null); return manager; } private EurekaClientConfigBean duplicateEurekaClientConfig() { EurekaClientConfigBean newConfig = new EurekaClientConfigBean(); newConfig.setFetchRegistry(false); newConfig.setEurekaServerPort(originalClientConfig.getEurekaServerPort()); newConfig.setAllowRedirects(originalClientConfig.isAllowRedirects()); newConfig.setAvailabilityZones(originalClientConfig.getAvailabilityZones()); newConfig.setBackupRegistryImpl(originalClientConfig.getBackupRegistryImpl()); newConfig.setServiceUrl(originalClientConfig.getServiceUrl()); return newConfig; } } 命令添加到另一个文件的开头来执行此操作的方法,因为它需要在第一个文件循环之后写入。

答案 2 :(得分:0)

您可以在第一个窗口中保存变量,而仅在另一个窗口中使用它们。

为什么不像这样从头开始使用导入?并以这种方式将变量传递到那里:

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())

答案 3 :(得分:0)

您可以在登录窗口中使用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()