将pyqt信号与其他类连接

2024-09-23 14:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我是否可以让pyqt信号触发另一个类的方法? 我试过各种方法,但都不走运。 我的目标是在单击(标记)文件室按钮时触发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()

Tags: 方法fromimportselfgetinitdefconnect
1条回答
网友
1楼 · 发布于 2024-09-23 14:29:52

首先,可以将pickFile函数设置为静态函数(不带self):

class get_rooms(MyApp):

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

    @staticmethod
    def pickFile(value, group_1):

然后您可以使用room_file_button.clicked信号;如果您想向该函数发送参数,可以使用lambda

^{pr2}$

相关问题 更多 >