簡體   English   中英

連接pyqt信號與另一個類

[英]Connec pyqt signal with another class

我是否可以獲取pyqt信號來觸發另一個類的方法? 我已經嘗試過各種方法,但是沒有運氣。 我的目的是在單擊(標記)room_file_button時在get_rooms中觸發pickFile()方法。

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()

首先,您可以將pickFile函數設置為靜態函數(不帶self):

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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM