如何从python中的另一个类访问变量

时间:2016-09-30 03:53:34

标签: class user-interface variables python-3.4 qt-designer

我想访问变量 lotId &在课程' InputDialog'中 qty_selected 并且可以在课堂上使用' Mainwindow'。我确实试图在网上找到解决方案但直到现在都无法解决。任何人都可以展示如何制作它吗?

这是我目前的代码:

from __future__ import division
from skimage.measure import compare_ssim as ssim
import matplotlib.pyplot as plt
import numpy as np
import sys
import os, glob
import cv2
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtGui import *
from inputdialog import Ui_inputDialog
from mainwindow import Ui_mainWindow
from tkinter import messagebox

class MainWindow(QtGui.QMainWindow, Ui_mainWindow):

  def __init__(self, class_input):
    QtGui.QMainWindow.__init__(self)
    Ui_mainWindow.__init__(self)
    Ui_inputDialog.__init__(self)
    self.setupUi(self)
    self.capture_button.clicked.connect(self.captureImage)
    self.display_button.clicked.connect(self.displayImage)
    self.deviceBox.activated.connect(self.selectDeviceCombo)
    self.startInspectionBtn.clicked.connect(self.enterLotID)
    self.display_button.clicked.connect(self.displayResults)
    #self.viewResultBtn.clicked.connect(self.viewResults)
    self.window2 = None


  def enterLotID(self): # Dialog box will ask user to enter lot ID and wafer qty
    if self.window2 is None:
        self.window2 = InputDialog(self)
    self.window2.isModal()
    self.window2.show()

# Program need to be loop through a image file in directory
  def displayImage(self, ):
    os.chdir('c:\\Users\\mohd_faizal4\\Desktop\\Python\\Testing' + '\\' + lotId )
    for lotId in glob.glob('*.jpeg'):
        print(lotId)

    sample_label = 'c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 6.jpg' # Sample image must read from current folder lot ID running the inspection
    self.sample_label.setScaledContents(True)
    self.sample_label.setPixmap(QtGui.QPixmap(sample_label))

  def selectDeviceCombo(self):
    self.var_Selected = self.deviceBox.currentText()
    #print('The user selected value now is:')
    print('Device = ' + self.var_Selected)

    if self.var_Selected.lower() == 'xf35':
      print("Great! Device Id is - " + self.var_Selected + '!')
      source_label ='c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 4.jpg'
      self.source_label.setScaledContents(True)
      self.source_label.setPixmap(QtGui.QPixmap(source_label))
    elif self.var_Selected.lower() == 'xf38':
      print("Great! Device Id is - " + self.var_Selected + '!')
      source_label ='c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 5.jpg'
      self.source_label.setScaledContents(True)
      self.source_label.setPixmap(QtGui.QPixmap(source_label))
    elif self.var_Selected.lower() == 'x38c':
      print("Great! Device Id is - " + self.var_Selected + '!')
      source_label ='c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 7.jpg'
      self.source_label.setScaledContents(True)
      self.source_label.setPixmap(QtGui.QPixmap(source_label))
    else:
      print("Pls select device id. It's compulsory field!")


  def captureImage(self):  # Capture image and display on 'Sample' column under Inspection

    cam = cv2.VideoCapture(0)

    i = 1
    while i < int(input(qty_selected)):
        ret, frame = cam.read()
        cv2.imshow('Please review an image', frame)

        if not ret:
            break
        k = cv2.waitKey(0)

        if k%256 == 27:
        # ESC pressed
         print("Escape hit, closing...")
         break
        if k % 256 == 32:
            # SPACE pressed
            img_name = "_{}.jpeg".format(i)
            #print (img_name)
            cv2.imwrite(img_name, frame)
            #cv2.imwrite(os.path.join(dirname, img_name), frame)
            print("{}".format(img_name))
            i += 1

    cam.release()
    cv2.destroyAllWindows()

  def displayResults(self): #Display image of wafer at 'Result' tab. Simultaneously with 'Inspect'
    label_vid01 = 'c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 7.jpg'
    self.label_vid01.setScaledContents(True)
    self.label_vid01.setPixmap(QtGui.QPixmap(label_vid01))


# A new class for user input dialog to enter lot information

class InputDialog (QtGui.QDialog, Ui_inputDialog):

  def __init__(self, parent):
    QtGui.QWidget.__init__(self, parent)
    self.setupUi(self)
    self.okButton.clicked.connect(self.addLotId)
    self.okButton.clicked.connect(self.selectWaferQty)

  def addLotId(self):
    lotId = str(self.strLotId.toPlainText())
    self.strLotId.setText(lotId)
    print(lotId)

    path = 'c:\\Users\\mohd_faizal4\\Desktop\\Python\\Testing' + '\\' + lotId
    if not os.path.exists(path):
            os.makedirs(path)

    else:
        messagebox.showwarning('Error','Please enter required information')
        QtGui.QMessageBox.show(self)


  # User require to select wafer quantity upon enter the lot ID
  def selectWaferQty(self):
    qty_selected = self.waferQty.currentText()
    #print ('The user selected value now is:')
    print ('Wafer Qty = ' + qty_selected)

    #if self.qty_selected() == '1':
    #  print('Great! Wafer Qty is - ' + self.qty_selected + '!')

    #else:
    #  print ('Pls select wafer quantity!')

if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)
   Window = MainWindow()
   Window.show()
   sys.exit()

1 个答案:

答案 0 :(得分:0)

在课程InputDialog下,分别将lotIdqty_selected更改为self.lotIdself.qty_selected。要在WindowMain课程中访问它,请尝试self.window2.lotIdself.window2.qty_selected