I have following code for my PyQt5 GUI:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QMainWindow, QGridLayout, QLayout, QTableWidget, QHeaderView, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QComboBox
from PyQt5.QtCore import QDate, Qt
from datetime import date, datetime
class CalendarWindow(QMainWindow):
def __init__(self, parent=None):
super(CalendarWindow, self).__init__(parent)
self.setUpUI()
def setUpUI(self):
self.setGeometry(50, 50, 1200, 700)
# Labels
self.label_events = QLabel(self)
self.label_new_event = QLabel(self)
self.label_event_name = QLabel(self)
self.label_start_date = QLabel(self)
self.label_repeat = QLabel(self)
self.label_description = QLabel(self)
# Labels parameters
self.label_events.setAlignment(Qt.AlignCenter)
self.label_events.setText("Wydarzenia danego dnia:")
# self.label_new_event.setAlignment(Qt.AlignCenter)
self.label_new_event.setText("Dodaj event:")
self.label_event_name.setText("Nazwa eventu:")
self.label_start_date.setText("Data eventu:")
self.label_repeat.setText("Powtarzalność eventu:")
self.label_description.setText("Opis:")
# TextEdits
self.text_event_name = QLineEdit(self)
self.text_start_date = QLineEdit(self)
self.text_description = QLineEdit(self)
# TextEdits parameters
# ComboBoxes
self.combo_repeat = QComboBox(self)
# ComboBoxes parameters
self.combo_repeat.addItems(
["No", "Day", "Week", "Two weeks", "Month", "Half a year", "Year"])
# Widgets
self.setWindowTitle("Python Calendar App")
self.calendar_widget = QCalendarWidget(self)
# User date select event
self.calendar_widget.clicked[QDate].connect(self.dateChanged)
self.events_list_widget = QTableWidget(self)
self.events_list_widget.setColumnCount(5)
self.events_list_widget.setHorizontalHeaderLabels(
["ID", "Name", "Date", "Repetition", "Description"])
# Columns stretched and fit
self.events_list_widget.horizontalHeader().setSectionResizeMode(
QHeaderView.Stretch)
self.main_widget = QWidget(self)
self.main_layout = QGridLayout(self.main_widget)
self.main_layout.sizeConstraint = QLayout.SetDefaultConstraint
self.main_layout.addWidget(self.calendar_widget, 0, 0)
self.init_sublayouts()
self.main_layout.addLayout(self.sublayout_events, 0, 1)
self.main_layout.addLayout(self.sublayout_new_event, 0, 2)
self.main_layout.setColumnStretch(0, 1)
self.main_layout.setColumnStretch(1, 3)
self.main_layout.setColumnStretch(2, 2)
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
def init_sublayouts(self):
# Events sublayout
self.sublayout_events = QVBoxLayout()
self.sublayout_events.addWidget(self.label_events)
self.sublayout_events.addWidget(self.events_list_widget)
# New event sublayout
self.sublayout_new_event = QVBoxLayout()
self.sublayout_new_event.addWidget(self.label_new_event)
self.sublayout_new_event_grid = QGridLayout()
self.sublayout_new_event_grid.addWidget(self.label_event_name, 1, 0)
self.sublayout_new_event_grid.addWidget(self.text_event_name, 1, 1)
self.sublayout_new_event_grid.addWidget(self.label_start_date, 2, 0)
self.sublayout_new_event_grid.addWidget(self.text_start_date, 2, 1)
self.sublayout_new_event_grid.addWidget(self.label_repeat, 3, 0)
self.sublayout_new_event_grid.addWidget(self.combo_repeat, 3, 1)
self.sublayout_new_event_grid.addWidget(self.label_description, 4, 0)
self.sublayout_new_event_grid.addWidget(self.text_description, 4, 1)
self.sublayout_new_event.addLayout(self.sublayout_new_event_grid)
def dateChanged(self, qdate):
print("Date changed to:", self.getDaysEvents())
selected_date = self.getDaysEvents()
# self.events_list_widget.addItem(selected_date.strftime("%d/%m/%y"))
def getDaysEvents(self):
selected_date = self.calendar_widget.selectedDate()
return selected_date.toPyDate()
if __name__ == '__main__':
app = QApplication(sys.argv)
foo = CalendarWindow()
foo.show()
sys.exit(app.exec_())
When you lanuch it, it looks like this:
I have a problem with that 3rd column. I want the Dodaj event: label to be at the top, and those textboxes with their labels to be not far away under it. The relevant code is in init_sublayouts under # New event sublayout comment. I tried to make those textboxes into QGridLayout and Dodaj event and this QGridLayout to QVBoxLabout, but u see what happened. I also tried to make every row of my QGridLayout a separate QHBoxLayout, and the result was pretty the same. Is there a way to do it? I did something similar in the column 2, where I have a QLabel stacked on top of QTableWidget, but in the column 3 it seems to be not working as expected.
void QBoxLayout::addStretch(int stretch = 0)
Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.
Try it:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QMainWindow, QGridLayout, QLayout, QTableWidget, QHeaderView, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QComboBox
from PyQt5.QtCore import QDate, Qt
from datetime import date, datetime
class CalendarWindow(QMainWindow):
def __init__(self, parent=None):
super(CalendarWindow, self).__init__(parent)
self.setUpUI()
def setUpUI(self):
self.setGeometry(50, 50, 1200, 700)
# Labels
self.label_events = QLabel(self)
self.label_new_event = QLabel(self)
self.label_event_name = QLabel(self)
self.label_start_date = QLabel(self)
self.label_repeat = QLabel(self)
self.label_description = QLabel(self)
# Labels parameters
self.label_events.setAlignment(Qt.AlignCenter)
self.label_events.setText("Wydarzenia danego dnia:")
self.label_new_event.setAlignment(Qt.AlignCenter) # +
self.label_new_event.setText("Dodaj event:")
self.label_event_name.setText("Nazwa eventu:")
self.label_start_date.setText("Data eventu:")
self.label_repeat.setText("Powtarzalność eventu:")
self.label_description.setText("Opis:")
# TextEdits
self.text_event_name = QLineEdit(self)
self.text_start_date = QLineEdit(self)
self.text_description = QLineEdit(self)
# TextEdits parameters
# ComboBoxes
self.combo_repeat = QComboBox(self)
# ComboBoxes parameters
self.combo_repeat.addItems(
["No", "Day", "Week", "Two weeks", "Month", "Half a year", "Year"])
# Widgets
self.setWindowTitle("Python Calendar App")
self.calendar_widget = QCalendarWidget(self)
# User date select event
self.calendar_widget.clicked[QDate].connect(self.dateChanged)
self.events_list_widget = QTableWidget(self)
self.events_list_widget.setColumnCount(5)
self.events_list_widget.setHorizontalHeaderLabels(
["ID", "Name", "Date", "Repetition", "Description"])
# Columns stretched and fit
self.events_list_widget.horizontalHeader().setSectionResizeMode(
QHeaderView.Stretch)
self.main_widget = QWidget(self)
self.main_layout = QGridLayout(self.main_widget)
self.main_layout.sizeConstraint = QLayout.SetDefaultConstraint
self.main_layout.addWidget(self.calendar_widget, 0, 0)
self.init_sublayouts()
self.main_layout.addLayout(self.sublayout_events, 0, 1)
self.main_layout.addLayout(self.sublayout_new_event, 0, 2)
self.main_layout.setColumnStretch(0, 1)
self.main_layout.setColumnStretch(1, 3)
self.main_layout.setColumnStretch(2, 2)
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
def init_sublayouts(self):
# Events sublayout
self.sublayout_events = QVBoxLayout()
self.sublayout_events.addWidget(self.label_events)
self.sublayout_events.addWidget(self.events_list_widget)
# New event sublayout
self.sublayout_new_event = QVBoxLayout()
self.sublayout_new_event.addWidget(self.label_new_event)
self.sublayout_new_event.addStretch(1) # +
self.sublayout_new_event_grid = QGridLayout()
self.sublayout_new_event_grid.addWidget(self.label_event_name, 1, 0)
self.sublayout_new_event_grid.addWidget(self.text_event_name, 1, 1)
self.sublayout_new_event_grid.addWidget(self.label_start_date, 2, 0)
self.sublayout_new_event_grid.addWidget(self.text_start_date, 2, 1)
self.sublayout_new_event_grid.addWidget(self.label_repeat, 3, 0)
self.sublayout_new_event_grid.addWidget(self.combo_repeat, 3, 1)
self.sublayout_new_event_grid.addWidget(self.label_description, 4, 0)
self.sublayout_new_event_grid.addWidget(self.text_description, 4, 1)
self.sublayout_new_event.addLayout(self.sublayout_new_event_grid)
self.sublayout_new_event.addStretch(20) # +
def dateChanged(self, qdate):
print("Date changed to:", self.getDaysEvents())
selected_date = self.getDaysEvents()
# self.events_list_widget.addItem(selected_date.strftime("%d/%m/%y"))
def getDaysEvents(self):
selected_date = self.calendar_widget.selectedDate()
return selected_date.toPyDate()
if __name__ == '__main__':
app = QApplication(sys.argv)
foo = CalendarWindow()
foo.show()
sys.exit(app.exec_())
Related
I wish to have a QGridLayout containing some informations that can be dynamicaaly changed. However, as it is, it will only superimpose the different informations on top of the previous one instead of replacing it. Surprisingly I couldn't find a method to remove or clean or such a widget from a grid. So what, can't it be dynamically changed ?...
Here is an example of code :
import sys
from PyQt5.QtWidgets import (
QApplication,
QGridLayout,
QPushButton,
QWidget,
QLabel
)
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QGridLayout Example")
# Create a QGridLayout instance
self.buttonA = QPushButton("A")
self.buttonB = QPushButton("B")
self.layout = QGridLayout()
# Add widgets to the layout
self.layout.addWidget(self.buttonA, 0, 1)
self.layout.addWidget(self.buttonB, 0, 2)
self.layout.addWidget(QPushButton("C"), 1, 0)
self.layout.addWidget(QPushButton("D"), 1, 1)
self.layout.addWidget(QPushButton("E"), 1, 2)
self.layout.addWidget(QPushButton("F"), 2, 0)
self.layout.addWidget(QPushButton("G"), 2, 1)
self.layout.addWidget(QPushButton("H"), 2, 2)
# Set the layout on the application's window
self.setLayout(self.layout)
self.buttonA.clicked.connect(self.change_label)
self.buttonB.clicked.connect(self.change_label2)
def change_label(self):
self.label = QLabel("Hello")
self.layout.addWidget(self.label, 0, 0)
def change_label2(self):
self.label = QLabel("Bonjour")
self.layout.addWidget(self.label, 0, 0)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
When one clicks on button A, I want to have displayed in box (0,0) "Hello". Then if one clicks on button B, I want to replace the text "Hello" with "Bonjour". However I can only have one text on top of the other which is obviously hardly readable then.
Can someone help me ?
Just use this code:
import sys
from PyQt5.QtWidgets import (
QApplication,
QGridLayout,
QPushButton,
QWidget,
QLabel
)
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QGridLayout Example")
# Create a QGridLayout instance
self.label = QLabel("Show text here")
self.buttonA = QPushButton("A")
self.buttonB = QPushButton("B")
self.layout = QGridLayout()
# Add widgets to the layout
self.layout.addWidget(self.label, 0, 0)
self.layout.addWidget(self.buttonA, 0, 1)
self.layout.addWidget(self.buttonB, 0, 2)
self.layout.addWidget(QPushButton("C"), 1, 0)
self.layout.addWidget(QPushButton("D"), 1, 1)
self.layout.addWidget(QPushButton("E"), 1, 2)
self.layout.addWidget(QPushButton("F"), 2, 0)
self.layout.addWidget(QPushButton("G"), 2, 1)
self.layout.addWidget(QPushButton("H"), 2, 2)
# Set the layout on the application's window
self.setLayout(self.layout)
self.buttonA.clicked.connect(self.change_label)
self.buttonB.clicked.connect(self.change_label2)
def change_label(self):
self.label.setText("Hello")
def change_label2(self):
self.label.setText("Bonjour")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I want to get a state from QCheckBox inside QTableWidget cell.
I've made example code for this issue.
import sys
from PyQt5.QtCore import Qt, QSettings
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from PyQt5.QtWidgets import QPushButton, QTableWidget, QCheckBox, QTextEdit
class TestUI(QWidget):
def __init__(self):
super().__init__()
self.btn = QPushButton("Get Data")
self.tbl = QTableWidget()
self.log = QTextEdit()
self.init_ui()
def init_ui(self):
self.btn.clicked.connect(self.get_data)
self.tbl.setFocusPolicy(Qt.NoFocus)
self.tbl.setMinimumHeight(255)
self.tbl.setMaximumHeight(255)
self.tbl.setRowCount(20)
self.tbl.setColumnCount(3)
self.tbl.horizontalHeader().setStretchLastSection(True)
self.tbl.setHorizontalHeaderLabels(["", "Option", "Value"])
self.tbl.resizeRowsToContents()
self.tbl.resizeColumnsToContents()
for row in range(20):
chk_box = QCheckBox()
chk_box.setCheckState(Qt.Unchecked)
cell = QWidget()
hlayout = QHBoxLayout()
hlayout.addWidget(chk_box)
hlayout.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
hlayout.setContentsMargins(0, 0, 0, 0)
cell.setLayout(hlayout)
self.tbl.setCellWidget(row, 0, cell)
vlayout = QVBoxLayout()
vlayout.addWidget(self.btn)
vlayout.addWidget(self.tbl)
vlayout.addWidget(self.log)
self.setLayout(vlayout)
self.show()
def get_data(self):
self.log.clear()
self.log.append(self.tbl.cellWidget(0, 0).isChecked())
self.log.append(self.tbl.cellWidget(0, 1).text())
self.log.append(self.tbl.cellWidget(0, 2).text())
if __name__ == "__main__":
APP = QApplication(sys.argv)
ex = TestUI()
sys.exit(APP.exec_())
How can I do this? I can't get the state through this code.
I think the code should be self.log.append(self.tbl.cellWidget(0, 0).????.isChecked()).
But I do not know exactly how to do it.
Please help me.
If the code that places the widget in column 0 is analyzed:
chk_box = QCheckBox()
chk_box.setCheckState(Qt.Unchecked)
cell = QWidget()
hlayout = QHBoxLayout()
hlayout.addWidget(chk_box)
hlayout.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
hlayout.setContentsMargins(0, 0, 0, 0)
cell.setLayout(hlayout)
self.tbl.setCellWidget(row, 0, cell)
It is noted that the widget set is not the QCheckBox but a QWidget, and the QCheckBox is the son of the QWidget so that information can be used to obtain it using the findChild() method. On the other hand, the item method may return None, so you should check if it is not, since it could throw an exception:
def get_data(self):
self.log.clear()
widget = self.tbl.cellWidget(0, 0)
if widget is not None:
chk_box = widget.findChild(QCheckBox)
if chk_box is not None:
self.log.append(str(chk_box.isChecked()))
it1 = self.tbl.item(0, 1)
self.log.append(it1.text() if it1 is not None else "")
it2 = self.tbl.item(0, 2)
self.log.append(it2.text() if it2 is not None else "")
A more elegant version of the above is to make the "cell" a custom widget that exposes the isChecked() method of the QCheckBox:
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.chk_box = QCheckBox()
self.chk_box.setCheckState(Qt.Unchecked)
hlayout = QHBoxLayout(self)
hlayout.addWidget(self.chk_box)
hlayout.setAlignment(Qt.AlignCenter)
hlayout.setContentsMargins(0, 0, 0, 0)
def isChecked(self):
return self.chk_box.isChecked()
class TestUI(QWidget):
def __init__(self):
super().__init__()
self.btn = QPushButton("Get Data")
self.tbl = QTableWidget()
self.log = QTextEdit()
self.init_ui()
def init_ui(self):
self.btn.clicked.connect(self.get_data)
self.tbl.setFocusPolicy(Qt.NoFocus)
self.tbl.setMinimumHeight(255)
self.tbl.setMaximumHeight(255)
self.tbl.setRowCount(20)
self.tbl.setColumnCount(3)
self.tbl.horizontalHeader().setStretchLastSection(True)
self.tbl.setHorizontalHeaderLabels(["", "Option", "Value"])
self.tbl.resizeRowsToContents()
self.tbl.resizeColumnsToContents()
for row in range(20):
cell = Widget()
self.tbl.setCellWidget(row, 0, cell)
vlayout = QVBoxLayout(self)
vlayout.addWidget(self.btn)
vlayout.addWidget(self.tbl)
vlayout.addWidget(self.log)
self.show()
def get_data(self):
self.log.clear()
widget = self.tbl.cellWidget(0, 0)
if widget is not None:
self.log.append(str(widget.isChecked()))
it1 = self.tbl.item(0, 1)
self.log.append(it1.text() if it1 is not None else "")
it2 = self.tbl.item(0, 2)
self.log.append(it2.text() if it2 is not None else "")
It can be deduced that you use the QWidget to center the QCheckBox inside the cell but I think it can be optimized to avoid creating widgets using a QProxyStyle, and then access the information through the checkState() method of the QTableWidgetItem:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QHBoxLayout,
QVBoxLayout,
QPushButton,
QTableWidget,
QTextEdit,
QTableWidgetItem,
QProxyStyle,
QStyle,
)
class ProxyStyle(QProxyStyle):
def subElementRect(self, element, option, widget):
r = super().subElementRect(element, option, widget)
if element == QStyle.SE_ItemViewItemCheckIndicator:
r.moveCenter(option.rect.center())
return r
class TestUI(QWidget):
def __init__(self):
super().__init__()
self.btn = QPushButton("Get Data")
self.tbl = QTableWidget()
self.log = QTextEdit()
self.init_ui()
def init_ui(self):
self.btn.clicked.connect(self.get_data)
proxy = ProxyStyle()
self.tbl.setStyle(proxy)
self.tbl.setFocusPolicy(Qt.NoFocus)
self.tbl.setFixedHeight(255)
self.tbl.setRowCount(20)
self.tbl.setColumnCount(3)
self.tbl.horizontalHeader().setStretchLastSection(True)
self.tbl.setHorizontalHeaderLabels(["", "Option", "Value"])
self.tbl.resizeRowsToContents()
self.tbl.resizeColumnsToContents()
for row in range(20):
it = QTableWidgetItem()
it.setCheckState(Qt.Checked)
self.tbl.setItem(row, 0, it)
vlayout = QVBoxLayout(self)
vlayout.addWidget(self.btn)
vlayout.addWidget(self.tbl)
vlayout.addWidget(self.log)
self.show()
def get_data(self):
self.log.clear()
it0 = self.tbl.item(0, 0)
self.log.append(str(it0.checkState() == Qt.Checked))
it1 = self.tbl.item(0, 1)
self.log.append(it1.text() if it1 is not None else "")
it2 = self.tbl.item(0, 2)
self.log.append(it2.text() if it2 is not None else "")
if __name__ == "__main__":
APP = QApplication(sys.argv)
ex = TestUI()
sys.exit(APP.exec_())
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()
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)
I'm using PYQT5 to create a GUI to display sliders, and I want an efficient way to create many sliders of the same type but with different names. Currently, I have to copy paste the same code to make each individual slider, so my code is unnecessarily long.
Here's what it looks like currently for 3 sliders:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
self.width = 1120
self.height = 527
MainWindow.resize(self.width, self.height)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.verticalSlider1 = QtWidgets.QSlider(self.shape_tab)
self.shape_minimum = 1
self.shape_maximum = 2000000
self.verticalSlider1.setRange(self.shape_minimum, self.shape_maximum)
self.verticalSlider1.setGeometry(QtCore.QRect(40, 40, 31, 251))
self.verticalSlider1.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider1.setMaximum(self.shape_maximum)
self.verticalSlider1.setObjectName("verticalSlider1")
self.verticalSlider2 = QtWidgets.QSlider(self.shape_tab)
self.shape_minimum = 1
self.shape_maximum = 2000000
self.verticalSlider2.setRange(self.shape_minimum, self.shape_maximum)
self.verticalSlider2.setGeometry(QtCore.QRect(80, 40, 31, 251))
self.verticalSlider2.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider2.setMaximum(self.shape_maximum)
self.verticalSlider2.setObjectName("verticalSlider2")
self.verticalSlider3 = QtWidgets.QSlider(self.shape_tab)
self.shape_minimum = 1
self.shape_maximum = 2000000
self.verticalSlider3.setRange(self.shape_minimum, self.shape_maximum)
self.verticalSlider3.setGeometry(QtCore.QRect(120, 40, 31, 251))
self.verticalSlider3.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider3.setMaximum(self.shape_maximum)
self.verticalSlider3.setObjectName("verticalSlider3")
I have to create 199 sliders in total, and they have the same properties. They only differ in names and the positioning in the UI (setGeometry(QtCore.QRect()). What's the best way to create this many sliders without just copy and pasting the same code 199 times and changing the name.
I thought of creating a Slider as an class object, but I don't think it works with the UI_MainWindow class. I'm not sure how a for loop would work either. Any thoughts or opinions would be greatly appreciated!
you just have to create a for-loop, and instead of having fixed names you could store them in a list:
...
self.sliders = []
shape_minimum, shape_maximum = 1, 2000000
r = QtCore.QRect(40, 40, 31, 251)
for i in range(199):
slider = QtWidgets.QSlider(self.shape_tab)
slider.setRange(shape_minimum, shape_maximum)
slider.setGeometry(r)
slider.setOrientation(QtCore.Qt.Vertical)
slider.setObjectName("verticalSlider{}".format(i+1))
r.translate(40, 0)
self.sliders.append(slider)
...
If you still want the sliders to have a name following the pattern you indicate in your code you can use setattr()
shape_minimum, shape_maximum = 1, 2000000
r = QtCore.QRect(40, 40, 31, 251)
for i in range(199):
slider = QtWidgets.QSlider(self.shape_tab)
slider.setRange(shape_minimum, shape_maximum)
slider.setGeometry(r)
slider.setOrientation(QtCore.Qt.Vertical)
name = "verticalSlider{}".format(i+1)
slider.setObjectName(name)
r.translate(40, 0)
setattr(self, name, slider)
...
Try it:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout, QGroupBox,
QMenu, QPushButton, QRadioButton, QVBoxLayout,
QWidget, QSlider, QLabel, QHBoxLayout, QScrollArea)
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle("PyQt5 Sliders")
self.resize(425, 392)
grid = QGridLayout()
scrollWidget = QWidget()
scrollWidget.setLayout(grid)
scrollArea = QScrollArea()
scrollArea.setWidgetResizable(True)
scrollArea.setWidget(scrollWidget)
self.mainLayout = QHBoxLayout()
self.mainLayout.addWidget(scrollArea)
self.setLayout(self.mainLayout)
for row in range(70):
grid.addWidget(self.createExampleGroup(row, 0), row, 0)
grid.addWidget(self.createExampleGroup(row, 1), row, 1)
grid.addWidget(self.createExampleGroup(row, 2), row, 2)
def createExampleGroup(self, row, column):
numSlider = row*2+column if row==0 else row*2+column+row
groupBox = QGroupBox("Slider {}".format(numSlider))
self.label = QLabel()
self.label.setObjectName("label{}".format(numSlider))
slider = QSlider(Qt.Horizontal)
name = "slider{}".format(numSlider)
slider.setObjectName(name)
setattr(self, name, self.label)
slider.setRange(1, 2000000)
slider.setFocusPolicy(Qt.StrongFocus)
slider.setTickPosition(QSlider.TicksBothSides)
slider.setTickInterval(200000)
slider.setSingleStep(1)
slider.valueChanged[int].connect(self.changevalue)
vbox = QVBoxLayout()
vbox.addWidget(self.label)
vbox.addWidget(slider)
vbox.addStretch(1)
groupBox.setLayout(vbox)
return groupBox
def changevalue(self, value):
sender = self.sender()
label = getattr(self, sender.objectName())
label.setText("{:>9,}".format(value))
if __name__ == '__main__':
app = QApplication(sys.argv)
clock = Window()
clock.show()
sys.exit(app.exec_())
When building a GUI application using python and the qt framework. I would like to reuse certain UI elements without defining them over and over again. In my example I would like to create a drop down menu defining the number of rows with y(number) and result(number)
Application example
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton,
QLineEdit, QGridLayout, QLabel)
from math import sqrt
class program(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Program')
flow = QLabel('x1')
pressuredrop_1 = QLabel('y1')
pressuredrop_2 = QLabel('y2 ')
kv = QLabel('result 1')
kv_2 = QLabel('result 2')
grid = QGridLayout()
grid.setSpacing(10)
self.setLayout(grid)
self.setGeometry(300,300,300,150)
self.btn = QPushButton('Calculate', self)
self.flow = QLineEdit(self)
self.pd = QLineEdit(self)
self.result = QLineEdit(self)
self.pd_2 = QLineEdit(self)
self.kvresult_2 = QLineEdit(self)
grid.addWidget(flow, 1,0)
grid.addWidget(self.flow, 1, 1)
grid.addWidget(pressuredrop_1, 2,0)
grid.addWidget(self.pd, 2, 1)
grid.addWidget(pressuredrop_2, 3,0)
grid.addWidget(self.pd_2, 3, 1)
grid.addWidget(kv, 2,2)
grid.addWidget(self.result, 2,3)
grid.addWidget(kv_2, 3,2)
grid.addWidget(self.kvresult_2, 3,3)
grid.addWidget(self.btn, 4,1)
self.btn.clicked.connect(self.calculate)
self.btn.clicked.connect(self.calculate_2)
self.show()