Starting another script using a button in tkinter
<p>I am trying to run another script within a script using a button in tkinter </p>

<p>I have tried two methods one being</p>

<pre><code>import os

class SeaofBTCapp(tk.Tk):
    #initilization
    def __init__(self, *args, **kwargs):      
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Battery Life Calculator")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (StartPage, PageOne, PageTwo, PageThree):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)


#raise each page to the front
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    def helloCallBack():
        os.system('python test2.py')

#the start page        
class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)
        button = ttk.Button(self, text="Visit Page         1",command=lambda:controller.show_frame(PageOne))                  
        button.pack()
        button1 = ttk.Button(self, text="Visit Page 2",command=lambda:controller.show_frame(PageTwo))                 
        button1.pack()
        button2 = ttk.Button(self, text="Visit Graph page",command=lambda:controller.show_frame(PageThree))              
        button2.pack()
        button3 = ttk.Button(self, text="execute test",command=lambda:controller.helloCallBack)               
        button3.pack()
</code></pre>

<p>No errors are given but when I hit execute nothing happens</p>

<p>The other method i tried was to</p>

<pre><code>import test2
</code></pre>

<p>but it runs the script automatically and it prints "i am a test"</p>

<p>I should note the script i am trying to call is simply a test and just prints "i am a test"</p>

<p>any help is appreciated! </p>

<p>thanks</p>

<p>****edit****</p>

<pre><code>import tkinter as tk
from tkinter import ttk

import test2







LARGE_FONT= ("Verdana", 12)

class SeaofBTCapp(tk.Tk):


#initilization
    def __init__(self, *args, **kwargs):      



        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "Battery Life Calculator")


        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo, PageThree):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)



#raise each page to the front
    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()







#the start page        
class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = ttk.Button(self, text="Visit Page 1",command=lambda:controller.show_frame(PageOne))

        button.pack()

        button1 = ttk.Button(self, text="Visit Page 2",command=lambda:controller.show_frame(PageTwo))

        button1.pack()

        button2 = ttk.Button(self, text="Visit Graph page",command=lambda:controller.show_frame(PageThree))

        button2.pack()

        button3 = ttk.Button(self, text="execute test",command=test2.main)

        button3.pack()











class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Page One", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Back to start page",command=lambda:controller.show_frame(StartPage))

        button1.pack()

        button1 = ttk.Button(self, text="Visit Page 2",command=lambda:controller.show_frame(PageTwo))

        button1.pack()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Page One", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Back to Start Page",command=lambda:controller.show_frame(StartPage))

        button1.pack()

        button1 = ttk.Button(self, text="Back to Page One",command=lambda:controller.show_frame(PageOne))

        button1.pack()

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Graph Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Back to Start Page",command=lambda:controller.show_frame(StartPage))

        button1.pack()




app = SeaofBTCapp()
app.mainloop()
</code></pre>

<p>test2.p is as follows</p>

<pre><code>def main():

    if __name__ == "__main__":
        print("Executing as main program")
        print("Value of __name__ is: ", __name__)
        main()
</code></pre>

<p>Your first method is not recommended for that. If you have another python script you should definitely import it.</p>

<p>About the second method:<br>
My guess is that the script <code>test2.py</code> is written without</p>

<pre><code>if __name__ == "__main__":
    main()
</code></pre>

<p>And that's why it shoot you message when you import it.</p>

python python-3.x user-interface tkinter