>source

pyqt5를 사용하여 선착순 스케줄러를 만들고 있습니다.

스케줄러 유형을 묻는 텍스트 상자를 포함하는 메인 창이 있는데, "GO"버튼을 클릭하면 두 번째 창이 열리고 프로세스 번호 및 버스트 시간과 같은 스케줄러에 대한 추가 정보를 요청합니다.

두 번째 창에서 "GO"버튼을 클릭하면 정보 처리 후 Gantt 차트가 나타납니다.

문제는 두 번째 창의 버튼이 전혀 작동하지 않는다는 것입니다.

유사한 질문 하지만 솔루션을 얻지 못했으며 JSON을 사용하지 않습니다.

코드 :

import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QMainWindow,QApplication, QComboBox, QDialog,QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,QVBoxLayoutQSpinBox, QTextEdit, QVBoxLayout
from PyQt5.QtGui import QIcon 
from PyQt5.QtCore import pyqtSlot
import matplotlib.pyplot as plt
import numpy as np
import linked_list
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        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)
        Self.fcfs.__init__()

class Window2(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My Program")
        self.setWindowIcon(QtGui.QIcon("download.jpg"))
        self.setGeometry(50,50,500,300)
        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,100)
        self.label4=QLabel(self)
        self.label4.setText("Burst Time")
        self.label4.move(120,100)
        self.label5=QLabel(self)
        self.label5.setText("Arrival Time")
        self.label5.move(200,100)
        self.names=QLineEdit(self)
        self.names.move(10,150)
        # self.names.resize(100,160)
        self.burst=QLineEdit(self)
        self.burst.move(120,150)
        #self.burst.resize(100,160)
        self.arrival=QLineEdit(self)
        self.arrival.move(250 ,150)
        #self.arrival.resize(100,160)
        #self.textEdit=QTextEdit(self)
        #self.textEdit.move(20,250)
        self.button=QPushButton("Go",self)
        self.button.move(0,200)
        self.button.clicked.connect(self.fcfs)
    def fcfs(self):
      //



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

<시간>
  • 답변 # 1

    부모를 두 번째 창으로 설정하십시오. 또한 Window2의 ____init____ 메소드를 두 번 호출합니다. 고정 코드는 다음과 같습니다.

    import sys
    from PyQt5 import QtGui
    from PyQt5.QtWidgets import (QMainWindow, QApplication, QComboBox,
    QDialog, QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox,
    QHBoxLayout, QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox,
    QTextEdit, QVBoxLayout)
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot
    import matplotlib.pyplot as plt
    import numpy as np
    import linked_list
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            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()
                fcfs = Window2(self)
                # Do not call __init__ explicitly below
                # fcfs.__init__()
    
    class Window2(QMainWindow):
        def __init__(self,parent=None):
            super().__init__(parent)
            # always try to set a 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.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, 100)
            self.label4 = QLabel(self)
            self.label4.setText("Burst Time")
            self.label4.move(120, 100)
            self.label5 = QLabel(self)
            self.label5.setText("Arrival Time")
            self.label5.move(200, 100)
            self.names = QLineEdit(self)
            self.names.move(10, 150)
            # self.names.resize(100,160)
            self.burst = QLineEdit(self)
            self.burst.move(120, 150)
            # self.burst.resize(100,160)
            self.arrival = QLineEdit(self)
            self.arrival.move(250, 150)
            # self.arrival.resize(100,160)
            # self.textEdit=QTextEdit(self)
            # self.textEdit.move(20,250)
            self.button = QPushButton("Go", self)
            self.button.move(0, 200)
            self.button.clicked.connect(self.fcfs)
        def fcfs(self):
            no_of_process = self.textbox.text()
            process_names = self.names.text()
            burst_time = self.burst.text()
            arrival_time = self.arrival.text()
            names_list = process_names.split()
            burst_list = burst_time.split()
            arrival_list = arrival_time.split()
       # integer conversion
            burst_list = [int(i) for i in burst_list]
            arrival_list = [int(i) for i in arrival_list]
            no_of_process = int(no_of_process)
            ls = LinkedList()
            i = 0
            while i < no_of_process:
                ls.append(names_list[i], burst_list[i], arrival_list[i])
                i = i + 1
            time = arrival_list[0]
            j = 0
            start = []
            end = []
            name = []
            while j < (ls.size()):
                while(time < arrival_list[j]):
                    time = time + 1
                    start.append(time)
                    time = time + burst_list[j]
                    end.append(time)
                    name.append(names_list[j])
                    j = j + 1
            waiting_time = 0
            k = 0
            average_waiting = 0
            while k < (ls.size()):
                waiting_time = waiting_time + \
                    end[k] - arrival_list[k] - burst_list[k]
                average_waiting = waiting_time / ls.size()
                k = k + 1
            x = name
            begin = np.array(start)
            end = np.array(end)
            plt.barh(range(len(begin)),  end - begin, left=begin)
            plt.yticks(range(len(begin)), x)
            #plt.text(0.6,1.5,('average waiting is ',average_waiting))
            plt.annotate(("average waiting is", average_waiting), xy=(0.5, 1.49))
            plt.show()
    
    def main():
        app = QApplication(sys.argv)
        main = MainWindow()
        main.show()
        sys.exit(app.exec_())
    if __name__ == '__main__':
        main()
    
    

    중요한 부분은 :

    def __init__(self,parent=None):
            super().__init__(parent)
    
    

관련 자료

  • 이전 vim 숫자 값 앞에 문자를 추가
  • 다음 android - 동적 기능의 활동을 찾을 수 없음 (ClassNotFound)