Girl
Girl

Reputation: 49

Button is not working on the second window of pyqt5

I'm making a first come first served scheduler using 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 .

When button "GO" on the second window is clicked, a Gantt chart should appear after processing the information.

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.

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

Upvotes: 0

Views: 745

Answers (1)

Omi
Omi

Reputation: 121

Set a parent to second window, also you are calling ____init____ method of Window2 twice. 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)

Upvotes: 1

Related Questions