繁体   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