【问题标题】:How can I reuse my function without any error?我怎样才能在没有任何错误的情况下重用我的函数?
【发布时间】:2023-01-24 21:23:18
【问题描述】:

如果单击提交按钮时我的 lineEdits 为空,我想让它们变成红色。添加一些东西后,我想让我的 lineEdit 变回白色。

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
import sys, os
from PyQt5.uic import loadUi
from BlurWindow.blurWindow import blur

def input_validate(self,lineedit):
    if self.lineedit.text() == '':
      self.lineedit.setStyleSheet('''
      QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,0,0,0.6); color:white; 
      }
      QLineEdit:focus {
      background-color:rgba(255,255,255,0.5);
      } ''')
      return False
    else:
      self.lineedit.setStyleSheet('''
      QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,255,255,0.6); color:white; 
      }
      QLineEdit:focus {
      background-color:rgba(255,255,255,0.5);
      } ''')
      return True

class MainWindow(QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()
    loadUi(r'D:\Workspace\Qt Designer\blur bg\blurtest.ui',self)
    self.setAttribute(Qt.WA_TranslucentBackground)
    blur(self.winId())
    self.setStyleSheet("background-color: rgba(0, 0, 0, 0)")

    self.browse1.clicked.connect(self.browsefile1)
    self.browse2.clicked.connect(self.browsefile2)
    self.submit.clicked.connect(self.submit1)
    self.setAttribute(Qt.WA_TranslucentBackground)
    blur(self.winId())
    self.setStyleSheet("background-color: rgba(0, 0, 0, 0)")

  def browsefile1(self):
    fname=QFileDialog.getOpenFileName(self,'Open File',os.getcwd())
    self.lineEdit1.setText(fname[0])
  def browsefile2(self):
    fname=QFileDialog.getOpenFileName(self,'Open File',os.getcwd())
    self.lineEdit2.setText(fname[0])
  def submit1(self):
    if not input_validate(self.lineEdit1) or not input_validate(self.lineEdit2):
      return
    print("successs")

app=QApplication(sys.argv)
mw=MainWindow()
mw.show()
sys.exit(app.exec_())

首先,我尝试将此输入验证分离为一个类:

class input_validate(QMainWindow):
  def __init__(self):
    super().__init__()
    if self.lineEdit.text() == '':
      self.lineEdit.setStyleSheet('''
      QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,0,0,0.6); color:white; 
      }
      QLineEdit:focus {
      background-color:rgba(255,255,255,0.5);
      } ''')
      return False
    else:
      self.lineEdit.setStyleSheet('''
      QLineEdit{font: 15pt "Yu Gothic"; border-style:none; border-bottom:1px solid rgba(255,255,255,0.6); color:white; 
      }
      QLineEdit:focus {
      background-color:rgba(255,255,255,0.5);
      } ''')
      return True

我得到的错误是
如果不是 input_validate(self.lineEdit1) 或者不是 input_validate(self.lineEdit2): 类型错误:输入验证。在里面() 采用 1 个位置参数,但给出了 2 个

所以我试着把它放在 MainWindow 类本身但是这次它说 如果不是 input_validate(self.lineEdit1) 或者不是 input_validate(self.lineEdit2): NameError:未定义名称“input_validate”

所以我把它放在 MainWindow 类之外,使其成为全局的,第一个代码块就是我所拥有的。

现在我的错误是 如果不是 input_validate(self.lineEdit1) 或者不是 input_validate(self.lineEdit2): 类型错误:input_validate() 缺少 1 个必需的位置参数:'lineedit'

我不知道我现在该如何解决这个问题。很长的路是分别用 linedit1 和 lineedit2 定义 input_validate 但我想弄清楚如何重用该代码。

另外我想知道继承 QMainWindow 而不是 QWidget 是否可以。现在这应该是一个窗口,因为它是第一个出现的窗口,所以我选择它作为主窗口。最多它会有一个成功的弹出窗口或单击提交时的错误弹出消息(我想我必须使用 QMessageBox 来实现)

【问题讨论】:

  • 你的input_validate 是一个班级。在 if 条件下使用它完全没有意义,尤其是因为该类没有这样的属性。使用标准函数,并将小部件作为参数传递。另外,请花点时间研究更多关于类和实例的知识,因为很明显你并不真正理解你在那里做什么,并且随机尝试这样的事情并不是很有效。
  • 再次嗨@musicamante 是的,我认为这不是在课堂上使用它的方法,所以我将它作为一个函数移到外面但现在收到错误消息说没有参数传递给该函数所以我很困惑现在该做什么

标签: python user-interface pyqt5


【解决方案1】:

你的方法有点偏离,可能比看起来有点复杂。您可以简单地在 MainWindow 类中定义一个函数,以便更简单和更容易执行。在您的第一个 sn-p 代码中,您在类之外定义了 input_validate

class MainWindow(QMainWindow):
    ... #Other stuff

    self.submit.clicked.connect(self.input_validate) #Triggers the function created below
    
    self.lineEdit.textChanged.connect(self.input_validate) 
    #It triggers whenever the text is changed in the lineEdit even with using setText,
    #If you want to just check when the user changes it, you can use textEdited. 

    def input_validate(self):
        if self.lineEdit.text() == '':
            ... #Set StyleSheet for Red
        else:
            ... #Set StyleSheet for White

此外,如果用户将其清除,lineEdit 将变为红色。如果您不希望它发生,您可以在input_validate 中添加一个参数,如果参数是True,则返回该函数,这样该函数将不会检查lineEdit.text()

而如果必须在connect中添加参数,则必须使用lambda,例如:self.lineEdit.textChanged.connect(lambda: input_validate())

【讨论】:

    猜你喜欢
    • 2023-01-02
    • 2021-02-28
    • 2018-02-20
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    相关资源
    最近更新 更多