Python:创建新流程 [英] Python : creating a new process
问题描述
我是Python的新手.我应该创建一个具有多个菜单的GUI.单击特定菜单后,应开始一个新过程,并且不应挂起用户界面.但是我无法做到这一点.在进行网络搜索之后,我编写了类似的代码.
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 .
在此代码中,我的目标是在不挂起UI的情况下激活深层打印"语句(单击(单击Me即可激活))
In this code my aim is to make the "print deep"statement active without hanging the UI (which gets active after clicking (Click Me ))
在这方面请帮助我.
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
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])
这篇关于Python:创建新流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!