Lornioiz
Lornioiz

Reputation: 415

Tkinter open an external exe file

I'm trying to setup an icon which, once pressed, open the file "sqlite3.exe" in order to manage a small database. If I input this from the command line:

os.system("sqlite3.exe")

the sqlite3 window is opened without problem, but if I embend the command in a Tk interface i'm not able to see the sqlite3 window (maybe it closes without trace?). I tried both os.system and subprocess with the same result.

from Tkinter import *
import os
import threading
import subprocess

class Application(object):
    def __init__(self, root):
        super(Application, self).__init__()
        self.root = root

        self.main_container = Frame(self.root)#, bg="bisque")
        self.main_container.pack(side=TOP, fill="both", expand='yes')


        self.button_1 = Button(self.main_container, text = "Os", relief=RAISED, command = lambda: self.os_open())
        self.button_1.pack()
        self.button_2 = Button(self.main_container, text = "Subprocess", relief=RAISED, command = lambda: self.sub_open())
        self.button_2.pack()

    def os_open(self):
        os.system("sqlite3.exe")

    def sub_open(self):
        exe = "sqlite3.exe"
        process = subprocess.Popen(exe, stdout=subprocess.PIPE)
        process.wait()

root = Tk()
app = Application(root)
root.mainloop()

Upvotes: 1

Views: 2143

Answers (1)

Moo
Moo

Reputation: 166

from Tkinter import * import os

    class App:
        def __init__(self, master):
            self.frame = Frame(master)
            self.b = Button(self.frame, text = 'Open', command = self.openFile)
            self.b.grid(row = 1)
            self.frame.grid()
        def openFile(self):
            os.startfile(_filepath_)

Upvotes: 3

Related Questions