简体   繁体   中英

how to open only 1 new window in tkinter after button click?

I have referred to this post Python tkinter: How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked? but i couldn't get my code to work. Could anyone please help me?

from tkinter import *
import os
import sys
import _thread
from s2 import work
from s1 import work2

root = Tk()
root.resizable(width=False, height=False)
root.title("Sample tkinter")
root.geometry(r"550x550")


def open_entry():
    os.system("data1.py")


def open_aisle():
    os.system("data2.py")

def close_entry():
    os.system("data10.py")

def close_aisle():
    os.system("data20.py")

def run():
    _thread.start_new_thread(work,())
    _thread.start_new_thread(work2,())

def openNewWindow():
    global createdb_btn
    newWindow = Toplevel(root)
    newWindow.resizable(width=False,height=False)
    newWindow.title("New Window")
    newWindow.geometry("300x300")
    create_front = Button(newWindow,text="Entry DB",relief=FLAT,bg="black",fg="white",command=open_entry)
    create_front.place(x=50,y=80)

    create_aisle = Button(newWindow,text="Aisle DB",relief=FLAT,bg="black",fg="white",command=open_aisle)
    create_aisle.place(x=145,y=80)



def openAnotherWindow():
    global removedb_btn
    otherWindow = Toplevel(root)
    otherWindow.resizable(width=False,height=False)
    otherWindow.title("New Window")
    otherWindow.geometry("300x300")
    remove_front = Button(otherWindow,text="Entry DB",relief=FLAT,bg="black",fg="white",command=close_entry)
    remove_front.place(x=50,y=80)

    remove_aisle = Button(otherWindow,text="Aisle DB",relief=FLAT,bg="black",fg="white",command=close_aisle)
    remove_aisle.place(x=145,y=80)

    removedb_btn = Button(root,text="Remove databases",relief=FLAT,bg="black",fg="white",state=DISABLED)
    removedb_btn.place(x=380,y=120)


camera_btn = Button(root,text="Open Cams",relief=FLAT,bg="black",fg="white",command=run)
camera_btn.place(x=50,y=120)

createdb_btn = Button(root,text="Create databases",relief=FLAT,bg="black",fg="white",command=openNewWindow)
createdb_btn.place(x=205,y=120)

removedb_btn = Button(root,text="Remove databases",relief=FLAT,bg="black",fg="white",command=openAnotherWindow)
removedb_btn.place(x=380,y=120)

root.mainloop()

I am trying to achieve this in both the openNewWindow function and openAnotherWindow function.

This is what I did. Changed the lines:

createdb_btn = Button(root,text="Create databases",relief=FLAT,bg="black",fg="white")
createdb_btn.config(command=lambda b=createdb_btn: (openNewWindow(b), b.config(state='disabled')))

and added

newWindow.protocol("WM_DELETE_WINDOW", lambda:(button.config(state='active'), newWindow.destroy()))

Inside newWindow function so that when you pass in the button that created the window as a variable and use that on the overwrite of opennewWindows function to reactivate it just before newWindow.destroy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM