连接按钮到PyQt5中的功能不起作用

时间:2016-02-20 19:27:09

标签: python pyqt5

我正在尝试制作

class User < ActiveRecord::Base     
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable

  def password_required?
    super if confirmed?
  end

  def password_match?
    self.errors[:password] << "can't be blank" if password.blank?
    self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
    self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
    password == password_confirmation && !password.blank?
  end

  # new function to set the password without knowing the current 
  # password used in our confirmation controller. 
  def attempt_set_password(params)
    p = {}
    p[:password] = params[:password]
    p[:password_confirmation] = params[:password_confirmation]
    update_attributes(p)
  end

  # new function to return whether a password has been set
  def has_no_password?
    self.encrypted_password.blank?
  end

  # Devise::Models:unless_confirmed` method doesn't exist in Devise 2.0.0 anymore. 
  # Instead you should use `pending_any_confirmation`.  
  def only_if_unconfirmed
    pending_any_confirmation {yield}
  end

    protected
    def confirmation_required?
      false
    end    
end

的工作。

我的main.py,控制器:

confirminformation_ui.pushButton.clicked.connect(self.abss)

我的pushButton无法使用self.abss。但适用于任何全局功能。例如:

import sys, pdb
from PyQt5 import QtWidgets, uic

class MainWindow():
    def __init__(self):
        main_ui.show()
        self.setSignals()
    def setSignals(self):
        main_ui.login_btn.clicked.connect(self.openConfirmation)
    def openConfirmation(self):

        Confirmation()
    def closeWindow(self):
        self.close()

class Confirmation():
    def __init__(self):
        main_ui.close()
        confirminformation_ui.show()
        self.setSignals()
    def setSignals(self):
        confirminformation_ui.pushButton.clicked.connect(self.abss)
    def abss(self):
        print("lineofabss")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_ui = uic.loadUi('main_window.ui')
    confirminformation_ui = uic.loadUi('confirm_information.ui')
    appointments_ui = uic.loadUi('appointment_list.ui')

    MainWindow = MainWindow()
    sys.exit(app.exec_())

我只定义了一个全局函数debug(),并将它连接到pushButton,它工作正常。 为什么self.abss不起作用?

confirminformation_ui

import sys, pdb
from PyQt5 import QtWidgets, uic

def debug():
    print("works")

class MainWindow():
    def __init__(self):
        main_ui.show()
        self.setSignals()
    def setSignals(self):
        main_ui.login_btn.clicked.connect(self.openConfirmation)
    def openConfirmation(self):

        Confirmation()
    def closeWindow(self):
        self.close()

class Confirmation():
    def __init__(self):
        main_ui.close()
        confirminformation_ui.show()
        self.setSignals()
    def setSignals(self):
        confirminformation_ui.pushButton.clicked.connect(debug)
    def abss(self):
        print("lineofabss")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_ui = uic.loadUi('main_window.ui')
    confirminformation_ui = uic.loadUi('confirm_information.ui')
    appointments_ui = uic.loadUi('appointment_list.ui')

    MainWindow = MainWindow()
    sys.exit(app.exec_())

output: works

appointmentlist_ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>503</width>
    <height>412</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QLabel" name="label_3">
     <property name="text">
      <string>Indian Mission Name</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QComboBox" name="comboBox"/>
   </item>
   <item>
    <widget class="QLabel" name="label_2">
     <property name="text">
      <string>Application ID (Filenumber)</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QLineEdit" name="lineEdit_2"/>
   </item>
   <item>
    <widget class="QLabel" name="label">
     <property name="text">
      <string>Date of birth</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QCalendarWidget" name="calendarWidget">
     <property name="locale">
      <locale language="Hindi" country="India"/>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>Submit</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

main_window.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>314</width>
    <height>471</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="1" column="0">
    <widget class="QListWidget" name="listWidget"/>
   </item>
   <item row="5" column="0">
    <widget class="QPushButton" name="pushButton_2">
     <property name="enabled">
      <bool>false</bool>
     </property>
     <property name="text">
      <string>Confirm the appointment</string>
     </property>
    </widget>
   </item>
   <item row="0" column="0">
    <widget class="QLabel" name="label">
     <property name="text">
      <string>Appointments:</string>
     </property>
    </widget>
   </item>
   <item row="4" column="0">
    <widget class="QComboBox" name="comboBox"/>
   </item>
   <item row="3" column="0">
    <widget class="QLabel" name="label_2">
     <property name="text">
      <string>Select Date:</string>
     </property>
    </widget>
   </item>
   <item row="2" column="0">
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>About</string>
     </property>
    </widget>
   </item>
   <item row="1" column="1">
    <widget class="QPushButton" name="pushButton_3">
     <property name="text">
      <string>Logout</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:0)

我不知道详细的细节,但这与对象self在函数调用中作为方法的参数一起传递有关。

您必须替换对方法的调用:

confirminformation_ui.pushButton.clicked.connect(self.abss)

具有lambda函数:

confirminformation_ui.pushButton.clicked.connect(lambda: self.abss())