如何在pyqt5中将QLabel的文本设置为变量?

时间:2019-04-10 16:34:17

标签: python pyqt5

我有两个窗口,主窗口打开第二个窗口,我要在其中将Qlabel设置为一个名为average_waiting的变量。

问题是我希望average_waiting是全局的,因为它将在整个代码中更改

所以当我尝试从主窗口打开第二个窗口时,这导致关闭整个程序

部分代码:

class MainWindow(QMainWindow):
def __init__(self,parent=None):
    super().__init__(parent)
    self.setWindowTitle("My Program")
    #self.setWindowIcon(QtGui.QIcon("download.jpg"))
    self.setGeometry(50,50,500,300)
    self.home()
    self.show()

def home(self):


    self.label2=QLabel(self)
    self.label2.setText("Type of Scheduler")
    self.label2.move(10,50)

    self.textbox2=QLineEdit(self)
    self.textbox2.move(100,50)


    self.button=QPushButton("Go",self)
    self.button.move(0,200)
    self.button.clicked.connect(self.runcode)

def runcode(self):
  schedular_type=self.textbox2.text()
  if(schedular_type=="FCFS"):
   self.close()
   self.fcfs=Window2(self,0)





//second window



 class Window2(QMainWindow):

def __init__(self,parent=None,average_waiting=0):
    super().__init__(parent)
    self.setWindowTitle("FCFS")
    #self.setWindowIcon(QtGui.QIcon("download.jpg"))
    self.setGeometry(50,50,500,300)
    self.average_waiting=average_waiting
    self.home()
    self.show()

def home(self):
    self.label1=QLabel(self)
    self.label1.setText("No of Processes")
    self.label1.move(10,0) #col ,row

    self.textbox=QLineEdit(self)
    self.textbox.move(100,0)


    self.label3=QLabel(self)
    self.label3.setText("Processess Names")
    self.label3.move(10,50)

    self.label4=QLabel(self)
    self.label4.setText("Burst Time")
    self.label4.move(10,100)

    self.label5=QLabel(self)
    self.label5.setText("Arrival Time")
    self.label5.move(10,150)

    self.names=QLineEdit(self)
    self.names.move(100,50)
    self.names.resize(1400,30)

    self.burst=QLineEdit(self)
    self.burst.move(100,100)
    self.burst.resize(1400,30)

    self.arrival=QLineEdit(self)
    self.arrival.move(100 ,150)
    self.arrival.resize(1400,30)
    #self.textEdit=QTextEdit(self)
    #self.textEdit.move(20,250)
    self.label6=QLabel(self)
    self.label6.setText("Waiting Time=",self.average_waiting)
    self.label6.move(0,250)

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import * 


class MainWindow(QMainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setWindowTitle("My Program")
        #self.setWindowIcon(QtGui.QIcon("download.jpg"))
        self.setGeometry(50,50,500,300)
        self.home()
        self.show()

    def home(self):
        self.label2 = QLabel(self)
        self.label2.setText("Type of Scheduler")
        self.label2.move(10,50)

        self.textbox2=QLineEdit(self)
        self.textbox2.move(100,50)
        self.button=QPushButton("Go",self)
        self.button.move(0,200)
        self.button.clicked.connect(self.runcode)

    def runcode(self):
        schedular_type=self.textbox2.text()
        if (schedular_type=="FCFS"):
#            self.close()
            self.hide()                                           # +++

            self.fcfs = Window2(self, 0)


class Window2(QMainWindow):
    def __init__(self, parent=None, average_waiting=0 ):
        super().__init__(parent)
        self.setWindowTitle("FCFS")
        #self.setWindowIcon(QtGui.QIcon("download.jpg"))
        self.setGeometry(50,50,500,300)
        self.average_waiting=average_waiting
        self.home()
        self.show()

    def home(self):
        self.label1=QLabel(self)
        self.label1.setText("No of Processes")
        self.label1.move(10,0) #col ,row
        self.textbox=QLineEdit(self)
        self.textbox.move(100,0)
        self.label3=QLabel(self)
        self.label3.setText("Processess Names")
        self.label3.move(10,50)
        self.label4=QLabel(self)
        self.label4.setText("Burst Time")
        self.label4.move(10,100)
        self.label5=QLabel(self)
        self.label5.setText("Arrival Time")
        self.label5.move(10,150)
        self.names=QLineEdit(self)
        self.names.move(100,50)
        self.names.resize(1400,30)
        self.burst=QLineEdit(self)
        self.burst.move(100,100)
        self.burst.resize(1400,30)
        self.arrival=QLineEdit(self)
        self.arrival.move(100 ,150)
        self.arrival.resize(1400,30)
        #self.textEdit=QTextEdit(self)
        #self.textEdit.move(20,250)
        self.label6 = QLabel(self)

#        self.label6.setText("Waiting Time=", self.average_waiting)
        self.label6.setText("Waiting Time = {}".format(self.average_waiting))   # +++

        self.label6.move(0,250)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)        
    form = MainWindow()
    form.show()
    sys.exit(app.exec_())

enter image description here