pyqt - 从其他.py文件更改小部件文本

时间:2017-03-19 15:31:55

标签: python pyqt

我有两个python文件,第一个是处理数据库相关的东西,第二个是导入该文件,所以我可以将它与PyQt一起使用。

问题是,如果我想更改第一个文件中的标签,它不会使应用程序崩溃。

我希望第一个文件能够更改标签的原因是在尝试连接到数据库时发生错误。

以下是我的代码的简短视图:

第一档。

from PyQt5.QtWidgets import QApplication, QWidget, QDialog
from datetime import *
from bs4 import BeautifulSoup as bs
import os
import sys
import DatabaseHandling

'''Convert UI file to Python'''
os.chdir("C:\\Users\Gianni Declercq\AppData\Local\Programs\Python\Python36-32\Scripts")
os.system("pyuic5.exe H:\QtProjects\\RPI1.ui -o H:\QtProjects\\RPI1_ui.py")

from RPI1_ui import Ui_Form  # import after recreation of py file
from RPI1_Sec import SecondWindow

'''Main program'''


class MainWindow(QWidget, Ui_Form):
    def __init__(self):
        super(MainWindow, self).__init__()
        #  Initialize variables
        self.dbu = DatabaseHandling.DatabaseUtility()
        self.spica_reference = None
        self.barcode = None
        self.msl = None
        self.package_body = None

        self.window2 = None

        # Get UI elements + resize window
        self.setupUi(self)
        self.resize(800, 480)

        # Define what should happen on button click
        self.btnQuit.clicked.connect(lambda: app.exit())
        self.btnInsert.clicked.connect(lambda: self.get_entry())
        self.btnTable.clicked.connect(lambda: self.new_window())

        # Style buttons
        self.btnQuit.setStyleSheet("background-color: red")
        self.btnInsert.setStyleSheet("background-color: green")
        self.btnTable.setStyleSheet("background-color: orange")

    def get_entry(self):
        try:
            self.spica_reference = self.txtReferentie.text()
            self.barcode = self.txtBarcode.text()
            self.msl = self.txtMsl.text()
            self.package_body = float(self.txtBodyPackage.text())

        except ValueError:
            self.lblShowInfo.setText("Please insert the correct values")
            self.lblShowInfo.setStyleSheet('color: red')
        else:
            self.dbu.mysql_connect()
            if self.dbu.cursor and self.dbu.cnx is not None:
                self.dbu.add_entry(self.spica_reference, self.barcode, self.msl, self.package_body, self.calc_floor_life)

第二档。

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

1 个答案:

答案 0 :(得分:0)

初始化时为DatabaseUtility提供“钩子函数”。

class MainWindow(QWidget, Ui_Form):
  def __init__():
    def ch_lab(text):
      self.lblShowInfo.setText(text)
    self.dbu = DatabaseHandling.DatabaseUtility(ch_lab)
class DatabaseUtility(Ui_Form):
  def __init__(cb):
    self.error_warn = cb
  #.. error happening
    self.error_warn('error')