简体   繁体   English

pyqt5的第二个窗口上的按钮不起作用

[英]Button is not working on the second window of pyqt5

I'm making a first come first served scheduler using pyqt5. 我正在使用pyqt5做一个先到先得的调度程序。

I have the main window which includes textbox asking the user for scheduler type, when button "GO" is clicked, the second window is opened which asks the user for more information about the scheduler like process numbers and burst time . 我有一个包含文本框的主窗口,询问用户输入调度程序的类型,单击按钮“ GO”后,打开第二个窗口,询问用户有关调度程序的更多信息,例如进程号和突发时间。

When button "GO" on the second window is clicked, a Gantt chart should appear after processing the information. 单击第二个窗口上的“ GO”按钮时,在处理完信息后应显示甘特图。

The problem is that the button on the second window is not working at all. 问题在于第二个窗口上的按钮根本不起作用。

I have found a similar question , but I didn't quite got the solution, plus I'm not using JSON. 我发现了一个类似的问题 ,但是我没有得到解决方案,而且我没有使用JSON。

Code: 码:

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()

Set a parent to second window, also you are calling ____init____ method of Window2 twice. 将父级设置为第二个窗口,您还要两次调用Window2的____init____方法。 Here is the fixed code : 这是固定代码:

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()

the crucial part is: 关键部分是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM