2014-05-15 2 views
2

사용자가 버튼 (button1)을 클릭하여 루틴을 시작할 수있는 Python의 QtGUI로 간단한 GUI를 프로그래밍 중이며 10 초 후에 다른 버튼 (button2)을 클릭하여 일부를 시작할 수있는 옵션이 있어야합니다 루틴.클릭 QPushButton 무시

작동하는 것 같다
self.button1=QtGui.QPushButton('Button1',self) 
self.button1.clicked.connect(self.button1clicked) 

self.button2=QtGui.QPushButton('Button2',self) 
self.button2.setEnabled(False) 
self.button2.clicked.connect(self.button2clicked) 

def button1clicked(self): 
    self.button2.setEnabled(False) 
    self.button2.clicked.disconnect() 
    self.timeNow = time.time() 
    self.enablebutton2() 

def enablebutton2(self): 
    while(True): 
     if time.time() - self.timeNow > 10: 
      self.button2.clicked.connect(self.button2clicked) 
      self.button2.setEnabled(True) 
      break 

def button2clicked(self): 
    someroutine() 

, 버튼을 초기에 사용 불가능하며, 내가 그것을 클릭 할 때 아무 일도 발생하지 않습니다하지만 내가 Button1을 클릭하고 10 초 단추 2 세 이상 후에 내가 전에했던 모든 클릭을 수신 : 나는 다음과 같은 시도 .

이런 일이 있어서는 안됩니다. button2를 사용하지 않도록 설정하면 어떻게 이러한 모든 클릭이 삭제되는지 어떻게 확인할 수 있습니까?

답변

0

button2를 비활성화 할 때 self.button2.clicked.disconnect()을 수행 할 필요가 없습니다. 그것은 그 자체로 충분합니다. 둘째, button1 슬롯에서 enableButton2()으로 전화하는 것은 잘못되었습니다. while 루프에서 주 스레드를 10 초 동안 차단하고 있습니다. 이것은 일을하는 잘못된 방법입니다.

대신 QTimer 설정 시간을 10 초 동안 사용하고 의 슬롯부터 시작해야합니다. 타이머 슬롯에서 button2를 활성화 할 수 있습니다. 다음, QTimer here를 참조하면 당신이 enablebutton2 다시 버튼을 만들 때 당신은 또한 한 번 ... 두 번 단추 2 연결을 쉽게 파이썬에서

QTimer *timer = new QTimer(this); 
connect(timer, SIGNAL(timeout()), this, SLOT(update())); 
timer->start(1000); 
+0

감사합니다. – Daniel

0

을 변환 할 수 있습니다 C++ 예입니다. 두 번째 연결 만 사용해야합니다. 그렇게하면 disconnect 문도 필요하지 않습니다.

또한 @Abhishek에 명시된 바와 같이 UI를 10 초 동안 차단할 때 QTimer을 사용해야합니다. C++ 코드는 다소 비슷해 보입니다.

QPushButton *button1 = new QPushButton("Button1"); 
connect(button1, SIGNAL(clicked()), this, SLOT(button1clicked())); 

QPushButton *button2 = new QPushButton("Button1"); 
button2->setEnabled(false); 

QTimer *timer = new QTimer(this); 
connect(timer, SIGNAL(timeout()), this, SLOT(enableButton2())); 

button1clicked() 
{ 
    timer->start(); 
} 

enableButton2() 
{ 
    button2->setEnabled(true); 
    connect(button2, SIGNAL(clicked()), this, SLOT(button2clicked())); 
} 

button2clicked() 
{ 
    someroutine(); 
} 
+0

그게 내가 실제로 한 일이지만, button2를 10 초 동안 사용할 수 있기를 바랄 때마다 button1을 클릭 할 때마다 button2를 연결했기 때문입니다. 그렇지 않으면 파이썬에서 동일합니다. – Daniel