2014-06-19 2 views
0

일부 물질 테스트를 자동화하기 위해 파이썬 2.7 tkinter GUI를 사용하고 있습니다. 테스트를 위해 여러 샘플을 테스트해야하며 샘플을 교환하는 데 시간이 걸립니다. "샘플 하나를 입력 한 다음 키보드에서 Enter 키를 누르십시오"와 같은 텍스트를 입력하고 Enter 키를 누를 때까지 기능이 일시 중지되도록하고 싶습니다. enter를 누르는 대신 tkinter 버튼을 사용할 수도 있습니다. 외부 라이브러리를 사용하지 않고 모든 아이디어? 하나의 단추를 누르면 루프를 시도하고 종료하는 while 루프를 시도했지만 루프가 실행 중이므로 단추가 등록되지 않습니다.파이썬 tkinter 이벤트에 대한 일시 중지

샘플 코드 (코드 많이 제거와 관련이 무엇 왼쪽) :

class App: 
def __init__(self,master): 

    #self.WILTRON = Wiltron_54128A_GPIB() 

    self.var = tk.StringVar() 
    self.var.trace("w",self.getTest) 

    self.okbutton = tk.IntVar() 
    self.okbutton.trace("w",self.OKbutton) 

    frame = Frame(master) 
    frame.pack() 

    #Initial GUI values 
    self.var.set('Choose Test') 
    testChoices = ['TEST'] 
    App.testOption = tk.OptionMenu(frame, self.var, *testChoices) 
    App.testOption.grid(row=0, column=0) 
    okButton = tk.Button(frame, text="  OK ", command=self.OKbutton).grid(row=2, column=1) 

#Test routine functions 
def getTest(self, *args): 
    test = self.var.get() 
    sf = "IC Network Analyzer" 
    root.title(sf) 

    ##### 
    if test == "TEST": 
     sample1 = self.WILTRON.Sample_Data() 
     print 'Change out sample then press OK' 
     #This is where I need to pause until the next sample has been inserted 
     sample2 = self.WILTRON.Sample_Data() 
     #ect. 
    ##### 

def OKbutton(self): 
    #Whatever I need to do to make the button exit the pause 
+0

수십 가지 방법이 있습니다. 일부 코드, 아마도 "샘플 테스트"기능을 게시 할 수 있다면 조언하기가 더 쉬울 것입니다. 예를 들어 - GUI를 작성하기 위해 클래스를 사용하고 있습니까? 아니면 일종의 전역 변수 시스템입니까? – Brionius

+0

안녕하세요 @Brionius, 관련 코드를 포함하도록 원래 게시물을 수정했습니다. 답장을 보내 주셔서 감사합니다! –

답변

1

사용 tkMessageBox

import Tkinter 
import tkMessageBox 

print "Sample #1" 
tkMessageBox.showinfo("Message", "Insert sample and press OK") 
print "Sample #2" 
+0

신난다, 고마워! 나는 그 해결책을 내가 상상했던 것보다 훨씬 좋아한다. –

2
다음

테스트를 시작하기 위해 콜백을 사용하는 작업 예를 들어, 그리고 콜백 각 샘플을 진행합니다.

import Tkinter as tk 

class App: 
    def __init__(self, master): 
     self.master = root 
     self.frame = tk.Frame(self.master) 

     self.okLabel = tk.Label(self.frame, text="Change out sample then press OK") 
     self.okButton = tk.Button(self.frame, text="  OK ", command=self.nextSample) 

     self.var = tk.StringVar() 
     self.var.set('Choose Test') 
     self.var.trace("w",self.beginTest) 
     testChoices = ['TEST'] 
     self.testOption = tk.OptionMenu(self.frame, self.var, *testChoices) 

     self.sampleNumber = 1 
     self.maxSamples = 5 
     self.testing = False 
     self.samples = [] 

     self.updateGUI() 

    def testSample(self): 
     # Do whatever you need to do to test your sample 
     pass 

    def beginTest(self, *args): # This is called when the user clicks the OptionMenu to begin the test 
     self.testing = True 

     sf = "IC Network Analyzer" 
     self.master.title(sf) 

     self.testOption.config(state=tk.DISABLED) 
     self.okButton.config(state=tk.NORMAL) 
     self.okLabel.config(text="Ready first sample, then press OK") 
     self.updateGUI() 

    def nextSample(self):   # This is called each time a new sample is tested. 
     if self.sampleNumber >= self.maxSamples: # If the maximum # of samples has been reached, end the test sequence 
      self.testing = False 
      self.sampleNumber = 1 
      self.testOption.config(state=tk.NORMAL) 
      self.okButton.config(state=tk.DISABLED) 

      # You'll want to save your sample data to a file or something here 

      self.samples = [] 

      self.updateGUI() 
     else: 
      self.sampleNumber += 1 
      if self.var.get() == "TEST": 
       self.samples.append(self.WILTRON.Sample_Data()) 
       self.okLabel.config(text="Switch to sample #"+str(self.sampleNumber)+" and press OK") 
       #At this point, the GUI will wait politely for you to push the okButton, before it runs this method again. 
       #ect. 
      ##### 

    def updateGUI(self): 
     self.frame.grid() 
     self.testOption.grid(row=1, column=1) 
     if self.testing: 
      self.okLabel.grid(row=2, column=1) 
      self.okButton.grid(row=3, column=1) 
     else: 
      self.okLabel.grid_remove() 
      self.okButton.grid_remove() 
     self.master.update_idletasks() 

root = tk.Tk() 
a = App(root) 
root.mainloop()