同时运行多个功能

时间:2016-06-02 12:18:54

标签: python python-2.7 tkinter

我是python的新手,我正在尝试编写一个小程序,它将更新查找文件并在目录中更新(这是有效的)

我面临的问题是我无法同时运行两个功能。例如,当代码搜索文件并替换它们时,还会显示一个小弹出窗口,显示系统正忙的文本消息。并且代码完成后显示弹出消息已完成。

!/usr/bin/python
# -*- coding: utf-8 -*-
#Shell script version 2
import os
import shutil
from Tkinter import *
import tkMessageBox
import tkSimpleDialog
from threading import Thread
import threading
import time
from multiprocessing import Process
import sys
from tkinter.ttk import *
import multiprocessing


Search = 'c:\\'
search_folder = ''


class popupWindow(object):
   def __init__(self,parent):
      top=self.top=Toplevel(parent)
      self.l=Label(top,text="Patch is running")
      self.l.pack()
      self.b=Button(top,text='Ok',command=self.cleanup)
      self.b.pack()
   def cleanup(self):
      #self.value=self.e.get()
      self.top.destroy()

class Example(Frame):
    def __init__(self, parent):
       Frame.__init__(self, parent)   

       self.parent = parent

       self.initUI()

    def initUI(self):
        photo = PhotoImage(file="C:\\IMAGE.gif") 
        self.parent.title("Off Line Playe Patch")
        self.style = Style()
        self.style.theme_use("default")

        self.pack(fill=BOTH, expand=1)

        W = Label(self,image=photo)
        W.photo = photo
        W.pack()
        #Button(tk,text='foo',command=bar).pack()
        quitButton = Button(self, text="Apply Patch",command=self.StPatch)
        quitButton.place(x=80, y=0)


    def StPatch(self):
        # Replaces shell.htm with correct file

        def FoundShell():

            for root, dirs, files in os.walk(search_folder):
            for file in files:
            print os.path.join(root,file)
            if file == 'shell.htm':
            print "found file"
            Shell = os.path.join(root,file)
            os.remove(Shell)
            shutil.copy2('./shell.htm',Shell)


     def Hello():
         self.w = popupWindow(self.parent)
         self.parent.wait_window(self.w.top)
         i = 0
         while i < 100000:
           print('hello world')
           i=i+1


        if os.path.exists('C:\Player - Office 2010'):
            print 'Found Folder'
            search_folder = 'C:\Player - Office 2010'
            tkMessageBox.showinfo("Update Done", "Update has been applyed successfuly  ", command=FoundShell())
        else:  # request user to enter path to Directory
            print 'not found'
            search_folder = tkSimpleDialog.askstring('Path', 'Please enter Path to Offline Player', )
            print search_folder
            tkMessageBox.showinfo("Update Done", "Update has been applyed successfuly  ", command=FoundShell())

        #t1 = threading.Thread(target=RunScript(self))
        #t1.start()
        #t1.join()
        #Hello()




    # the join means wait untill it finished

root = Tk()
app = Example(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

import thread
from template import *
from graph import *

try:
   thread.start_new_thread(main,())
   thread.start_new_thread(graph,())
except KeyboardInterrupt:
    thread.exit()

while 1:
   pass

这是如何使用multiThreads一次调用2个函数的示例。在没有参数的情况下调用函数main&#34; ()&#34;来自模板文件模块和graph功能,没有参数&#34; ()&#34;来自模块图。

并在名为 run.py

的辅助文件中使用此功能

while 1让线程保持运行。直到KeyboardInterrupt被处理。

希望这可以提供帮助!