2016-03-07 67 views
0

是否有可能讓我得到pyqt信號觸發另一個類的方法? 我嘗試了各種各樣,但沒有運氣。 我的目標是在單擊room_file_button(標記)時觸發get_rooms中的pickFile()方法。連接pyqt信號與另一個類

import sys 
from PyQt4 import QtCore, QtGui, uic 
import openpyxl 
from openpyxl import load_workbook 
from openpyxl.styles import Protection 
import xlrd 
import csv 
import os 
import re 

class MyApp(QtGui.QMainWindow, Ui_MainWindow):  

    def __init__(self): 
     QtGui.QMainWindow.__init__(self) 
     Ui_MainWindow.__init__(self) 
     self.setupUi(self) 


     self.room_file_button.clicked.connect(get_rooms.pickFile) # this one 
     self.radioButton_1.clicked.connect(self.onRadioButton1)  
     self.radioButton_2.clicked.connect(self.onRadioButton2)  
     self.radioButton_3.clicked.connect(self.onRadioButton3) 
     self.spinBox.valueChanged.connect(self.valuechange) 


class first_file(MyApp):    
    def __init__(self): 
     MyApp.__init__(self) 

     some methods .... 


class get_rooms(MyApp): 

    def __init__(self): 
     MyApp.__init__(self) 

    def pickFile(self, value, group_1): 
     print 'yipeee !' 
     xy = 0 
     while True: 
      filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.') 
      if filename == '' and xy < 2: 
       print(" ") 
       xy = xy + 1 
       continue 
      elif filename != '': 
       break 
      else: 
       sys.exit() 
+2

爲什麼是類'first_file'和'get_rooms'從'MyApp'繼承?你想達到什麼目的?請多給點信息。 – DreyFax

回答

-1

首先,可以設置pickFile用作靜態函數(無自我):

class get_rooms(MyApp): 

    def __init__(self): 
     MyApp.__init__(self) 

    @staticmethod 
    def pickFile(value, group_1): 

,然後就可以使用room_file_button.clicked信號;如果你想將參數發送到該功能,您可以使用lambda

self.room_file_button.clicked.connect(lambda: get_rooms.pickFile(myValue,myGrupe)) 
+0

您可以創建get_rooms的實例並將該方法用作插槽。所以_you **必須將'pickFile'設置爲靜態函數_錯誤!也不會有通過調用的任何參數,因爲信號沒有定義參數。 Lambda定義沒有價值。 – DreyFax

+0

是的,**必須**錯了。我不知道pyQt4,但在pyQt5(和python 3)這個代碼工作正常。 –

+0

也許我應該說使用PYQT4和Python 2.7即時通訊 – Stevo