【问题标题】:Button is not working on the second window of pyqt5按钮在 pyqt5 的第二个窗口上不起作用
【发布时间】:2019-04-06 18:30:08
【问题描述】:

我正在使用 pyqt5 制作先到先得的调度程序。

我的主窗口包括询问用户调度程序类型的文本框,当单击“GO”按钮时,会打开第二个窗口,询问用户有关调度程序的更多信息,如进程号和突发时间。

当点击第二个窗口上的“GO”按钮时,处理完信息后会出现甘特图。

问题是第二个窗口上的按钮根本不起作用。

我找到了similar question,但我并没有完全得到解决方案,而且我没有使用 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()

【问题讨论】:

  • 请改进缩进
  • 有没有办法包含文件本身?
  • 我已经编辑了缩进

标签: python button pyqt5


【解决方案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)

【讨论】:

  • 但是您能告诉我我们为什么要进行这些修改吗?
  • 它确实可以在所有窗口上使用你想要创建的所有窗口,但一定要设置父母(如果可能的话,直接父母),即使失败,在你的类之外创建一个函数(Qt继承) 当您从前一个窗口调用它时,它会启动另一个窗口,这可能会起作用。如果它对你有用,请投票给我的答案。 !
  • 我做了第三个窗口,很像第二个窗口,有你编辑的两行,但它不起作用,你知道为什么吗?
  • 也许我知道为什么,但前提是你给我看代码!
  • @Girl 查看我的更新答案,确保在您创建的每个自定义窗口或小部件中都这样做。
猜你喜欢
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 2021-06-03
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多