简体   繁体   English

Python:创建新流程

[英]Python : creating a new process

I am new to Python . 我是Python的新手。 I was supposed to create a GUI with multiple menus . 我应该创建一个具有多个菜单的GUI。 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 )) 在此代码中,我的目的是在不挂起UI的情况下激活“深层打印”语句(单击(单击我)后将激活)

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()
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 proc.py

while True:
    print('deep')

parent_proc.py parent_proc.py

import subprocess
import sys    

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

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

You don't need multiple threads to start an external process. 您不需要多个线程即可启动外部进程。 Popen() doesn't wait for the child process to end. Popen()不等待子进程结束。 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 . 这是一个完整的代码示例,该示例在不使用thread的情况下在子流程的开始/结束处启动/停止tkinter进度栏


Unrelated: to run while True: print 'deep'; time.sleep(1) 不相关: while True: print 'deep'; time.sleep(1)运行while True: print 'deep'; time.sleep(1) 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: while True: print 'deep'; time.sleep(1)而不阻塞GUI(假设print('deep')不会长时间阻塞),可以使用parent.after()方法:

def OnButtonClick(self):
    self._step()    

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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