Open window on button click, close current window?

636 Views Asked by At

I have been searching for a solution for this issue, but I can't seem to find a viable answer.

I have the following code to open another tkinter script on button click:

# Program to Open on Button Click
def tkinter1():
    ret = os.system('python C:\filepath\new_script.py"')
    if ret:
        # record updated, reload data
        treeview_preview()

b1 = Button(master, text="New Window", command=tkinter1)

My problem I am facing is that I want the current window to close on the button click and only keep the new window open.

I know it is possible. I have this instance with many different windows and I seem to be stuck. The unfortunate thing is that I have an entire different script for different parts of the beta software and the only way I could successfully run all of them is to access them as stated above.

I tried using the if ret: exit() command at the end with the same result. I have windows opening over and over again.

It seems simple, but I haven't been programming tkinter script for too long.(I still have a lot to learn)

All help is appreciated.

1

There are 1 best solutions below

1
On

You can use master.withdraw(). I made your code runnable with a few lines. Ok lets say your fixed main code is this:

import tkinter as tk
from tkinter import *
from seccond import *
from multiprocessing import Process
import os
def main():
    master = tk.Tk()
    # Program to Open on Button Click
    def tkinter1():
        master.withdraw()
        master.destroy()
        ret = Process(target=treeview_preview())
        ret.start()
        #if ret:
        #    # record updated, reload data
        #    treeview_preview()
        #commented out bc i don't know what it is
    b1 = Button(master, text="New Window", command=tkinter1)
    b1.pack()
    master.mainloop()
if __name__ == '__main__':
    main()

the secondary code name seccond.py in the same folder as the main code is called as an external function but also as a separate process.

import tkinter as tk
def treeview_preview():
    root = tk.Tk()
    S = tk.Scrollbar(root)
    T = tk.Text(root, height=4, width=50)
    S.pack(side=tk.RIGHT, fill=tk.Y)
    T.pack(side=tk.LEFT, fill=tk.Y)
    S.config(command=T.yview)
    T.config(yscrollcommand=S.set)
    quote = """this is the seccond window."""
    T.insert(tk.END, quote)
    tk.mainloop()

The code will remove the window but not the terminal box as it will use it for the second script.