2013-06-19 6 views
4

나는 파이썬에서 초보자이며, 제 첫 번째 언어입니다. 나는 내가 끝내야 할 필요가 있음을 깨닫기에는 너무 빨리 커지고있는 무언가를 받았다. 나는 거의 끝났어. 이 시점에서 나는 주 메뉴, 테스트를 실행하는 주 메뉴에서 선택 가능한 옵션 인 대화 상자 및 테스트를 실행하는 다중 스레드 인스턴스로 사용되는 대화 상자를 만들었으며 "기다려주십시오" 상자를 클릭하고 테스트를 완료하면 테스트 완료를 선언하는 다른 대화 상자가 나타납니다.Tkinter : 멀티 스레드 인스턴스 호출

내 문제 : "테스트 실행"대화 상자에서 멀티 스레드 인스턴스를 호출하는 버튼을 만들려고합니다. 다른 사람들의 도움을 받아 구문 분석 한 코드에서 "테스트 실행"대화 상자에서 어떤 클래스가 인스턴스화되는지를 볼 수 없습니다.

스레딩 구현이 잘못되었다고 생각하기 시작했습니다. 그러나 방법이 있어야합니다.

전화를 걸려고하는 모듈입니다.

from slice_setup import SLICE_SETUP 
import Tkinter as tk 
import threading 
import Queue 


class GuiPart: 
    def __init__(self, master, queue): 
     self.queue = queue 
     self.master = master 
     self.master.geometry("300x100+400+250") 
     self.master.title("RSAM BCT") 
     tk.Label(master, text="REDCOM SLICE", fg="red").pack() 
     tk.Label(master, text="BCT - Basic Configuration Test", fg= "red").pack() 
     tk.Label(master, text="Please wait...", fg= "black").pack() 
     tk.Label(master, text="Estimated time: 3 min 6 sec", fg= "black").pack() 

    def processIncoming(self): 
     while self.queue.qsize(): 
      try: 
       text = self.queue.get(0) 
       Complete(self.master, text) 
      except Queue.Empty: 
       pass 

class ThreadedClient: 
    def __init__(self, master): 
     self.master = master 
     self.queue = Queue.Queue() 
     self.gui = GuiPart(master, self.queue) 
     self.running = True 
     self.thread = threading.Thread(target=self.workerThread1) 
     self.thread.start() 
     self.periodicCall() 

    def periodicCall(self): 
     self.gui.processIncoming() 
     if not self.running: 
      return 
     self.master.after(100, self.periodicCall) 

    def workerThread1(self): 
     obj_rcs = SLICE_SETUP() 
     obj_rcs.SLICE() 
     self.queue.put("Configuration Complete!") 
     self.running = False 

class Complete(tk.Toplevel): 
    def __init__(self, master=None, completetext=""): 
     tk.Toplevel.__init__(self, master) 
     self.geometry("400x300+400+250") 
     self.title("RSAM BCT") 
     tk.Label(self, text="REDCOME SLICE", fg="red").pack() 
     tk.Label(self, text="BCT - Basic Configuration Test", fg="red").pack() 
     tk.Label(self, text=completetext, fg="dark green").pack() 
     tk.Label(self, text="Trunk 1: Port 1: Phone 1: 760-450-4500", fg="black").pack() 
     tk.Label(self, text="Trunk 1: Port 2: Phone 2: 760-450-4501", fg="black").pack() 
     tk.Button(self, text=" Exit ", command=self.destroy).pack() 


if __name__ == "__main__": 
    root = tk.Tk() 
    client = ThreadedClient(root) 
    root.mainloop() 

내가에서 호출하려고 곳이다 :이 클래스는 여전히 사소한 오류를 가지고 있지만 난 그냥이 문제를 먼저 해결 싶어

import sys 
import Tkinter as Tk() 
from bct_pleasewait import ???? 
import threading 
import Queue 
import time 
sGui = Tk() 

class slice_menu: 

    def runtest(self): 
     obj_wait = ???? 
     obj_wait.???? 

    def slicemenu(self): 
     sGui.geometry("400x300+400+250") 
     sGui.title("RSAM BCT") 
     Label(sGui, text= "REDCOM SLICE", fg="red").pack() 
     Label(sGui, text= "BCT - Basic Configuration Test", fg= "red").pack() 
     Label(sGui, text= "-Ensure you are logged off of HyperTerminal", fg= "black").pack() 
     Label(sGui, text= "-Turn on your REDCOM SLICE unit", 
     fg= "black").pack() 
     Label(sGui, text= "-Please connect your laptop to SLICE CONSOLE", fg= "black").pack() 
     Label(sGui, text= "-This configuration will take 3 minutes", fg= "black").pack() 
     Button(sGui, text = "  Run  ", command = self.runtest).pack() 
     Button(sGui, text = " Exit test ", command = sGui.destroy).pack() 
     sGui.mainloop() 

.

+0

나는 문제가 무엇인지 더 정확하게 생각해야한다고 생각합니다. 귀하의 질문에 대한 동기 부여가 충분하지 않은 것 같아서, 여기에있는 많은 사람들이 조금 더 명확하면 문제를 쉽게 해결할 수 있다고 생각합니다. –

답변

0

구체적인 답변은 아니지만 귀하의 질문은 매우 광범위합니다. 명심해야 할

몇 가지 포인트 :

  • 의 Tk는 스레드로부터 안전하지 않습니다. 즉, 은 주 스레드에서 Tk 호출을 호출해야합니다. 다른 스레드가 non-gui 작업을하도록하는 것이 좋습니다.
  • CPython에서 전역 인터프리터 잠금 (GIL)으로 인해 한 번에 하나의 스레드 만 파이썬 바이트 코드를 실행할 수 있습니다. 따라서 GUI가 아닌 스레드로 인해 GUI가 응답하지 않을 수 있습니다.

한 번에 하나의 테스트 만 실행하고 해당 테스트를 작은 조각으로 나눌 수 있다면 시간 초과 (alarm handler라고도 함)를 사용할 수 있습니다. 이 처리기는 약간의 작업을 수행하고 상태를 저장하며 진행 대화 상자를 업데이트 한 다음 종료하고 다시 호출 대기합니다.

테스트가 오랜 시간 실행되고 GUI 응답을 유지하려면 스레딩 대신 multiprocessing을 사용하는 것이 좋습니다. 다른 프로세스에서 테스트를 시작하고 Queues 및 Semaphores 등과 같은 것을 사용하여 GUI 프로세스와 비 -qui 프로세스간에 통신하십시오.