2017-03-19 10 views
0

私は2つのpythonファイルを持っています。最初はデータベース関連のものを扱い、もう1つはそのファイルをインポートしてPyQtで使用できます。pyqt - 他の.pyファイルからウィジェットテキストを変更する

問題は、最初のファイルでラベルを変更したい場合、アプリケーションがクラッシュするだけで動作しないことです。

私は最初のファイルをラベルを変更できるようにする理由は、DBに接続しようとするとエラーが発生した場合です。

ここに私のコードの略図があります:

ファーストファイル。

​​

2番目のファイル。

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) 

答えて

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') 
関連する問題