Deepak Kumar
Deepak Kumar

Reputation: 109

Python : creating a new process

I am new to Python . I was supposed to create a GUI with multiple menus . On clicking a particular menu a new process should start and it should not hang the User Interface . But i am not able to achieve that . After web searches I have made a similar kind of code .

In this code my aim is to make the "print deep"statement active without hanging the UI (which gets active after clicking (Click Me ))

Please help me in this regard .

import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"Enter text here.")

        button = Tkinter.Button(self,text=u"Click me !",
                                command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.labelVariable = Tkinter.StringVar()
        label = Tkinter.Label(self,textvariable=self.labelVariable,
                              anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW')
        self.labelVariable.set(u"Hello !")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())       
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnButtonClick(self):
        while True:
            print 'deep'
    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

Upvotes: 0

Views: 1543

Answers (2)

jfs
jfs

Reputation: 414875

You don't need multiple threads to start an external process. Popen() doesn't wait for the child process to end. It returns immediately. Here's a complete code example that starts/stops a tkinter progressbar on the start/end of a subprocess without using threads .


Unrelated: to run while True: print 'deep'; time.sleep(1) without blocking the GUI (assuming print('deep') does not block for long), you could use parent.after() method:

def OnButtonClick(self):
    self._step()    

def _step(self):
    print('deep')
    self.parent.after(1000, self._step) # call _step in a second

Upvotes: 0

Pavel Ryvintsev
Pavel Ryvintsev

Reputation: 1008

import threading

class simpleapp_tk(Tkinter.Tk):
    # .............................

    def OnButtonClick(self):
       thr = threading.Thread(target=self.print_deep)
       thr.start()

    def print_deep(self):
       while True:
          print 'deep'

If you want to create a new process you need to create separate script that will run in separate process. I still thing that better solution will be using threads.

proc.py

while True:
    print('deep')

parent_proc.py

import subprocess
import sys    

class simpleapp_tk(Tkinter.Tk):
    # .............................

    def OnButtonClick(self):
        subprocess.Popen(args=['python', 'proc.py'], stdout=sys.stdout])

Upvotes: 1

Related Questions