PyQtGraph opens its own window, not main app window - python

I'm using Qt Designer to build an application in Python, which needs to show a graph in the main application window with buttons and the main program. I'm using PyQtGraph for the plot.
Here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(668, 458)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.labelx = QtWidgets.QLabel(self.centralwidget)
self.labelx.setGeometry(QtCore.QRect(50, 20, 581, 231))
self.labelx.setAutoFillBackground(True)
self.labelx.setFrameShape(QtWidgets.QFrame.Panel)
self.labelx.setFrameShadow(QtWidgets.QFrame.Sunken)
self.labelx.setLineWidth(3)
self.labelx.setMidLineWidth(3)
self.labelx.setText("")
self.labelx.setScaledContents(True)
self.labelx.setObjectName("labelx")
self.label_15 = QtWidgets.QLabel(self.centralwidget)
self.label_15.setGeometry(QtCore.QRect(350, 280, 91, 16))
self.label_15.setObjectName("label_15")
self.lineEdit_13 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_13.setGeometry(QtCore.QRect(230, 280, 113, 20))
self.lineEdit_13.setObjectName("lineEdit_13")
self.lineEdit_14 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_14.setGeometry(QtCore.QRect(230, 310, 113, 20))
self.lineEdit_14.setText("")
self.lineEdit_14.setObjectName("lineEdit_14")
self.label_16 = QtWidgets.QLabel(self.centralwidget)
self.label_16.setGeometry(QtCore.QRect(350, 310, 91, 16))
self.label_16.setObjectName("label_16")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(10, 390, 111, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(120, 390, 111, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit_15 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_15.setGeometry(QtCore.QRect(440, 280, 113, 20))
self.lineEdit_15.setText("")
self.lineEdit_15.setObjectName("lineEdit_15")
self.label_17 = QtWidgets.QLabel(self.centralwidget)
self.label_17.setGeometry(QtCore.QRect(560, 280, 91, 16))
self.label_17.setObjectName("label_17")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(550, 390, 111, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(230, 340, 111, 23))
self.pushButton_4.setObjectName("pushButton_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 668, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton_2.clicked.connect(self.lineEdit_13.clear)
self.pushButton_2.clicked.connect(self.lineEdit_14.clear)
self.pushButton_2.clicked.connect(self.lineEdit_15.clear)
self.pushButton_4.clicked.connect(self.plotwid)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_15.setText(_translate("MainWindow", "Total Expenses"))
self.label_16.setText(_translate("MainWindow", "Total Income"))
self.pushButton.setText(_translate("MainWindow", "Add Expenses"))
self.pushButton_2.setText(_translate("MainWindow", "Cleasr All"))
self.label_17.setText(_translate("MainWindow", "Total Savings"))
self.pushButton_3.setText(_translate("MainWindow", "Exit"))
self.pushButton_4.setText(_translate("MainWindow", "Make Graph"))
def plotwid(self):
# define the data
title = "Savings Calculator"
t = int(8)
# y values to plot by line 1
y = [1, 5, 6, 8, 6, 11, 14, 13, 18, 30]
# y values to plot by line 2
y2 = [1, 1, 5, 8, 9, 11, 16, 17, 14, 30]
x = range(0, 10)
# create plot window object
plt = pg.plot()
# showing x and y grids
plt.showGrid(x=True, y=True)
# adding legend
plt.addLegend()
# set properties of the label for y axis
plt.setLabel('left', 'Amount of savings and income', units='y')
# set properties of the label for x axis
plt.setLabel('bottom', 'Months to save', units='s')
# setting horizontal range
plt.setXRange(0, 12)
# setting vertical range
plt.setYRange(0, 1000)
# setting window title
plt.setWindowTitle(title)
# ploting line in green color
line1 = plt.plot(x, y, pen='g', symbol='x', symbolPen='g',
symbolBrush=0.2, name='green')
# ploting line2 with blue color
line2 = plt.plot(x, y2, pen='b', symbol='o', symbolPen='b',
symbolBrush=0.2, name='blue')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Problem
It always opens in its own window. How can I set it to open in a window within my application window?

First of all, do not modify the code generated by QtDesigner (for more information read here and here) so you must regenerate the file: pyuic5 design.ui -o design.py -x`
Considering the above, the solution is to place the PlotWidget inside "labelx" through a layout (another solution can be to promote the widget):
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from design import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.pushButton_2.clicked.connect(self.lineEdit_13.clear)
self.pushButton_2.clicked.connect(self.lineEdit_14.clear)
self.pushButton_2.clicked.connect(self.lineEdit_15.clear)
self.pushButton_4.clicked.connect(self.plotwid)
lay = QtWidgets.QVBoxLayout(self.labelx)
lay.setContentsMargins(0, 0, 0, 0)
self.plt = pg.plot()
lay.addWidget(self.plt)
def plotwid(self):
title = "Savings Calculator"
t = 8
# y values to plot by line 1
y = [1, 5, 6, 8, 6, 11, 14, 13, 18, 30]
# y values to plot by line 2
y2 = [1, 1, 5, 8, 9, 11, 16, 17, 14, 30]
x = range(0, 10)
self.plt.showGrid(x=True, y=True)
# adding legend
self.plt.addLegend()
# set properties of the label for y axis
self.plt.setLabel("left", "Amount of savings and income", units="y")
# set properties of the label for x axis
self.plt.setLabel("bottom", "Months to save", units="s")
# setting horizontal range
self.plt.setXRange(0, 12)
# setting vertical range
self.plt.setYRange(0, 1000)
# setting window title
self.plt.setWindowTitle(title)
# ploting line in green color
line1 = self.plt.plot(
x, y, pen="g", symbol="x", symbolPen="g", symbolBrush=0.2, name="green"
)
# ploting line2 with blue color
line2 = self.plt.plot(
x, y2, pen="b", symbol="o", symbolPen="b", symbolBrush=0.2, name="blue"
)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Related

Embedding KDE plot on PyQt5 though Geoplot library

I want to embed Kernel Density Estimation plot on PyQt5 GUI using Geoplot library. I found out how to embed simple matplot through canvas and followed same steps for KDE plot but all in vain. GUI opens but doesn't embed KDE. When I close qt GUI, the result is executed at the end of code.
Here is my implemented code:
#Imports
import csv
import os
import sys
from os.path import dirname, realpath,join
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow,QVBoxLayout,QAction,QFileDialog, QWidget
from PyQt5.uic import loadUiType
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from PyQt5 import QtCore, QtGui, QtWidgets
import subprocess
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import rcParams
import seaborn as sns
%matplotlib inline
import numpy as np
from pandas.plotting import register_matplotlib_converters
from datetime import datetime
from statsmodels.tsa.seasonal import STL
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
class Ui_Healthcare(object):
def setupUi(self, Healthcare):
Healthcare.setObjectName("Healthcare")
Healthcare.resize(779, 567)
self.centralwidget = QtWidgets.QWidget(Healthcare)
self.centralwidget.setObjectName("centralwidget")
self.All_Gen = QtWidgets.QRadioButton(self.centralwidget)
self.All_Gen.setGeometry(QtCore.QRect(30, 60, 95, 20))
self.All_Gen.setObjectName("All_Gen")
self.Male = QtWidgets.QRadioButton(self.centralwidget)
self.Male.setGeometry(QtCore.QRect(30, 90, 95, 20))
self.Male.setObjectName("Male")
self.Female = QtWidgets.QRadioButton(self.centralwidget)
self.Female.setGeometry(QtCore.QRect(30, 120, 95, 20))
self.Female.setObjectName("Female")
self.Undefined = QtWidgets.QRadioButton(self.centralwidget)
self.Undefined.setGeometry(QtCore.QRect(30, 150, 95, 20))
self.Undefined.setObjectName("Undefined")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(20, 20, 161, 301))
self.groupBox.setStyleSheet("border-color: rgb(0, 0, 0);")
self.groupBox.setObjectName("groupBox")
self.groupBox_2 = QtWidgets.QGroupBox(self.groupBox)
self.groupBox_2.setGeometry(QtCore.QRect(0, 180, 161, 111))
self.groupBox_2.setObjectName("groupBox_2")
self.Age_1 = QtWidgets.QSpinBox(self.groupBox_2)
self.Age_1.setGeometry(QtCore.QRect(30, 30, 42, 22))
self.Age_1.setObjectName("Age_1")
self.Age_2 = QtWidgets.QSpinBox(self.groupBox_2)
self.Age_2.setGeometry(QtCore.QRect(100, 30, 42, 22))
self.Age_2.setObjectName("Age_2")
self.label = QtWidgets.QLabel(self.groupBox_2)
self.label.setGeometry(QtCore.QRect(80, 30, 21, 16))
self.label.setObjectName("label")
self.Age_3 = QtWidgets.QSpinBox(self.groupBox_2)
self.Age_3.setGeometry(QtCore.QRect(30, 70, 42, 22))
self.Age_3.setObjectName("Age_3")
self.label_3 = QtWidgets.QLabel(self.groupBox_2)
self.label_3.setGeometry(QtCore.QRect(80, 70, 21, 16))
self.label_3.setObjectName("label_3")
self.Age_4 = QtWidgets.QSpinBox(self.groupBox_2)
self.Age_4.setGeometry(QtCore.QRect(100, 70, 42, 22))
self.Age_4.setObjectName("Age_4")
self.Age_Lim1 = QtWidgets.QCheckBox(self.groupBox_2)
self.Age_Lim1.setGeometry(QtCore.QRect(10, 30, 16, 20))
self.Age_Lim1.setText("")
self.Age_Lim1.setObjectName("Age_Lim1")
self.Age_Lim2 = QtWidgets.QCheckBox(self.groupBox_2)
self.Age_Lim2.setGeometry(QtCore.QRect(10, 70, 16, 20))
self.Age_Lim2.setText("")
self.Age_Lim2.setObjectName("Age_Lim2")
self.TimeSeries = QtWidgets.QCheckBox(self.centralwidget, clicked = lambda: self.timeseries())
self.TimeSeries.setGeometry(QtCore.QRect(30, 360, 111, 20))
self.TimeSeries.setObjectName("TimeSeries")
self.KDE = QtWidgets.QCheckBox(self.centralwidget, clicked = lambda: self.KDEplot())
self.KDE.setGeometry(QtCore.QRect(30, 390, 121, 20))
self.KDE.setObjectName("KDE")
self.Histograms = QtWidgets.QCheckBox(self.centralwidget)
self.Histograms.setGeometry(QtCore.QRect(30, 420, 121, 20))
self.Histograms.setObjectName("Histograms")
self.Anomalies = QtWidgets.QCheckBox(self.centralwidget)
self.Anomalies.setGeometry(QtCore.QRect(30, 600, 121, 20))
self.Anomalies.setObjectName("Anomalies")
self.AllPatients = QtWidgets.QCheckBox(self.centralwidget)
self.AllPatients.setGeometry(QtCore.QRect(30, 330, 121, 20))
self.AllPatients.setObjectName("AllPatients")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(180, -10, 20, 731))
self.line.setFrameShape(QtWidgets.QFrame.VLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.KDE_plot = QtWidgets.QFrame(self.centralwidget)
self.KDE_plot.setGeometry(QtCore.QRect(200, 40, 381, 411))
self.KDE_plot.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.KDE_plot.setFrameShadow(QtWidgets.QFrame.Raised)
self.KDE_plot.setObjectName("KDE_plot")
#create a horizontal layout
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.KDE_plot)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
#Canvas here
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas)
#end of canvas
#Add canvas
self.horizontalLayout_4.addWidget(self.canvas)
self.horizontalLayout_4.addWidget(self.toolbar)
#end of horizontal layout
self.Smallframe1 = QtWidgets.QFrame(self.centralwidget)
self.Smallframe1.setGeometry(QtCore.QRect(600, 40, 161, 111))
self.Smallframe1.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.Smallframe1.setFrameShadow(QtWidgets.QFrame.Raised)
self.Smallframe1.setObjectName("Smallframe1")
self.smallframe2 = QtWidgets.QFrame(self.centralwidget)
self.smallframe2.setGeometry(QtCore.QRect(600, 170, 151, 111))
self.smallframe2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.smallframe2.setFrameShadow(QtWidgets.QFrame.Raised)
self.smallframe2.setObjectName("smallframe2")
self.smallframe3 = QtWidgets.QFrame(self.centralwidget)
self.smallframe3.setGeometry(QtCore.QRect(600, 310, 151, 121))
self.smallframe3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.smallframe3.setFrameShadow(QtWidgets.QFrame.Raised)
self.smallframe3.setObjectName("smallframe3")
Healthcare.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Healthcare)
self.menubar.setGeometry(QtCore.QRect(0, 0, 779, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
self.menuHelp = QtWidgets.QMenu(self.menubar)
self.menuHelp.setObjectName("menuHelp")
Healthcare.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Healthcare)
self.statusbar.setObjectName("statusbar")
Healthcare.setStatusBar(self.statusbar)
self.actionImport = QtWidgets.QAction(Healthcare)
self.actionImport.setObjectName("actionImport")
self.actionOpen = QtWidgets.QAction(Healthcare)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(Healthcare)
self.actionSave.setObjectName("actionSave")
self.menuFile.addAction(self.actionImport)
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuHelp.menuAction())
self.retranslateUi(Healthcare)
QtCore.QMetaObject.connectSlotsByName(Healthcare)
path = self.actionImport.triggered.connect(self.browsefiles)
def retranslateUi(self, Healthcare):
_translate = QtCore.QCoreApplication.translate
Healthcare.setWindowTitle(_translate("Healthcare", "MainWindow"))
self.All_Gen.setText(_translate("Healthcare", "All"))
self.Male.setText(_translate("Healthcare", "Male"))
self.Female.setText(_translate("Healthcare", "Female"))
self.Undefined.setText(_translate("Healthcare", "Undefined "))
self.groupBox.setTitle(_translate("Healthcare", "Demographics"))
self.groupBox_2.setTitle(_translate("Healthcare", "Age"))
self.label.setText(_translate("Healthcare", "To"))
self.label_3.setText(_translate("Healthcare", "To"))
self.TimeSeries.setText(_translate("Healthcare", "Time Series"))
self.KDE.setText(_translate("Healthcare", "Kernel Density"))
self.Histograms.setText(_translate("Healthcare", "Histograms"))
self.Anomalies.setText(_translate("Healthcare", "Anomalies"))
self.AllPatients.setText(_translate("Healthcare", "All patients"))
self.menuFile.setTitle(_translate("Healthcare", "File"))
self.menuEdit.setTitle(_translate("Healthcare", "Edit"))
self.menuHelp.setTitle(_translate("Healthcare", "Help"))
self.actionImport.setText(_translate("Healthcare", "Import"))
self.actionImport.setStatusTip(_translate("Healthcare", "Import Dataset"))
self.actionOpen.setText(_translate("Healthcare", "Open"))
self.actionSave.setText(_translate("Healthcare", "Save"))
def browsefiles(self):
path=QFileDialog.getOpenFileName(None, 'Open file', os.getenv('HOME'), "CSV(*.csv)")
print(path[0])
self.df = pd.read_csv(path[0], index_col = "Date")
def KDEplot(self):
self.figure.clear()
lahore = gpd.read_file(r"C:\Users\drfak\Toolkit\Lahore towns uc and vill\LH_TOWNS-2006_GEO_AREAS.shp")
dataset = pd.read_csv("ABCD.csv")
dataset = gpd.GeoDataFrame(dataset, geometry=gpd.points_from_xy(dataset.Longitude, dataset.Latitude))
gplt.kdeplot(dataset, cmap='Reds', fill = True, clip=lahore)
gplt.polyplot(lahore , ax=ax, zorder =1)
self.canvas.draw()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Healthcare = QtWidgets.QMainWindow()
ui = Ui_Healthcare()
ui.setupUi(Healthcare)
Healthcare.show()
sys.exit(app.exec_())

issue with QScrollArea

I am trying to add some check boxes in scroll area.but for me it is not working properly.i am new in python could anyone help?
import PyQt5
from PyQt5 import QtCore, QtGui, QtWidgets
from configparser import ConfigParser
import paramiko
import threading
import os
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
class Ui_MainWindow(object):
def __init__(self, parent=None):
self.configur = ConfigParser()
self.configur.read('ServerConfigurationDetails.ini')
self.serverName=self.configur.sections()
print(self.serverName)
self.getComponents = self.configur.options("Component")
self.sLen = len(self.serverName)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(951, 634)
font = QtGui.QFont()
font.setPointSize(10)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(150, 220, 91, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(250, 220, 231, 31))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(700, 40, 101, 31))
self.pushButton.setObjectName("pushButton")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(140, 260, 681, 81))
self.groupBox.setObjectName("groupBox")
self.radioButton = QtWidgets.QRadioButton(self.groupBox)
self.radioButton.setGeometry(QtCore.QRect(10, 30, 201, 20))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_2.setGeometry(QtCore.QRect(220, 30, 201, 20))
self.radioButton_2.setObjectName("radioButton_2")
self.checkBox = QtWidgets.QCheckBox(self.groupBox)
self.checkBox.setEnabled(False)
self.checkBox.setGeometry(QtCore.QRect(10, 50, 231, 31))
self.checkBox.setChecked(True)
self.checkBox.setObjectName("checkBox")
self.radioButton_3 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_3.setGeometry(QtCore.QRect(420, 30, 171, 20))
self.radioButton_3.setObjectName("radioButton_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(280, 40, 411, 31))
self.lineEdit.setObjectName("lineEdit")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(130, 40, 131, 31))
self.label_3.setObjectName("label_3")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(140, 125, 661, 21))
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 110, 681, 101))
self.scrollArea.setAccessibleDescription("")
self.scrollArea.setWidgetResizable(False)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 679, 99))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
c=10
n=0
x = 10
y = 10
print(self.sLen)
for i in range(self.sLen):
if(n==4):
n=0
x=10
y=y+20
self.checkbox1 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.checkbox1.setGeometry(QtCore.QRect(x+1, y+1, 161, 20))
self.checkbox1.setText(self.serverName[i])
self.checkbox1.setObjectName(self.serverName[i])
print(x)
print(n)
n=n+1
if(n==4):
x=10
y = y+20
else:
x= x+180
## self.checkBox_2 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_2.setGeometry(QtCore.QRect(10, 10, 161, 20))
## self.checkBox_2.setObjectName("checkBox_2")
## self.checkBox_3 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_3.setGeometry(QtCore.QRect(180, 10, 131, 20))
## self.checkBox_3.setObjectName("checkBox_3")
## self.checkBox_4 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_4.setGeometry(QtCore.QRect(350, 10, 151, 20))
## self.checkBox_4.setObjectName("checkBox_4")
## self.checkBox_5 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_5.setGeometry(QtCore.QRect(530, 10, 141, 20))
## self.checkBox_5.setObjectName("checkBox_5")
## self.checkBox_6 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_6.setGeometry(QtCore.QRect(10, 30, 161, 20))
## self.checkBox_6.setObjectName("checkBox_6")
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea_2 = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea_2.setGeometry(QtCore.QRect(140, 380, 681, 191))
self.scrollArea_2.setWidgetResizable(True)
self.scrollArea_2.setObjectName("scrollArea_2")
self.scrollAreaWidgetContents_2 = QtWidgets.QWidget()
self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 679, 189))
self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(70, 340, 811, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 951, 26))
self.menubar.setObjectName("menubar")
self.menuhelp = QtWidgets.QMenu(self.menubar)
self.menuhelp.setObjectName("menuhelp")
MainWindow.setMenuBar(self.menubar)
self.actionAbout = QtWidgets.QAction(MainWindow)
self.actionAbout.setObjectName("actionAbout")
self.menuhelp.addAction(self.actionAbout)
self.menubar.addAction(self.menuhelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt;\">Component</span></p></body></html>"))
self.comboBox_2.setItemText(0, _translate("MainWindow", "eComm"))
self.comboBox_2.setItemText(1, _translate("MainWindow", "workproc"))
self.pushButton.setText(_translate("MainWindow", "Search"))
self.groupBox.setTitle(_translate("MainWindow", "Other Details"))
self.radioButton.setText(_translate("MainWindow", "Only Search in Archive"))
self.radioButton_2.setText(_translate("MainWindow", "Also Search in Archive"))
self.checkBox.setText(_translate("MainWindow", "Auto Download"))
self.radioButton_3.setText(_translate("MainWindow", "Only Search in Log"))
self.label_3.setText(_translate("MainWindow", "<html><head/><body><p>Search String<span style=\" color:#ff0000;\">*</span></p></body></html>"))
## self.checkBox_2.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_3.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_4.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_5.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_6.setText(_translate("MainWindow", "CheckBox"))
self.menuhelp.setTitle(_translate("MainWindow", "Help"))
self.actionAbout.setText(_translate("MainWindow", "About"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
as you can see the scroll area is not showing properly.i have some 20 servers to show.this server list is coming from ini file and i need to show the check boxes in that scroll area.
you need to use QLayout to arrange you widgets. check out
Qt Designer -> "Widget Box" docker -> "Layouts" section
For the scrollarea part:
create scrollarea scroll_area = QScrollArea()
create view widget you want to put inside scrollarea base_widget = QWidget()
install view layout for view widget base_layout = QVBoxLayout(base_widget)
install view widget into scrollarea scroll_area.setWidget(base_widget)
fill in objects with view layout base_layout.addWidget(QLabel('foo')) base_layout.addWidget(QPushButton('bar')) base_layout.addLayout(my_sub_layout) etc.

adding and removing widgets in PyQt

What I'm doing is about that when I clicked the date of the
calendar ,then it will display the class of UI_tab which is a tabwidget ,including two tabs.each tab includes some labels and others.However it
cant't work.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Tab(object):
# create tabwidget
def __init__(self,parentw,*args):
print(args)
print(parentw)
super().__init__()
self.font = QtGui.QFont()
self.font.setFamily("仿宋")
self.font.setPointSize(10)
self.font.setItalic(False)
self.font.setUnderline(False)
self.w=['title0','title1',
'creator0','creator1',
'c_time0','c_time1',
'user0','user1',
'd_time0','d_time1',
'detail0']
self.wt=['title','creator','create_time',
'viewer','finish_time','detail']
self.tabWidget = QtWidgets.QTabWidget(parentw)
self.tabWidget.setGeometry(QtCore.QRect(50, 10, 500, 2500))
self.tabWidget.setMinimumSize(QtCore.QSize(400, 400))
self.tabWidget.setTabsClosable(False)
self.tabWidget.setMovable(True)
self.tabWidget.setObjectName("tabWidget")
self.initTab()
# create tabs in tabwidget
def initTab(self,tabnum=2):
self.tabnum=tabnum
print('inittab')
self.tabs={}
for i in range(self.tabnum):
self.tabs[str(i)]=QtWidgets.QWidget()
self.tabs[str(i)].setObjectName("tab"+str(i))
self.tabWidget.addTab(self.tabs[str(i)], "")
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabs[str(i)]),str(i))
self.initw_in_tab(self.tabs[str(i)])
print(self.tabs)
# add labels,textbrowser in tabs
def initw_in_tab(self,parentw):
w_in_tab={}
print('initw_in_tab')
for i in self.w:
w_in_tab[i]=QtWidgets.QLabel(parentw)
w_in_tab[i].setFont(self.font)
w_in_tab[i].setLayoutDirection(QtCore.Qt.LeftToRight)
w_in_tab[i].setAutoFillBackground(False)
w_in_tab[i].setFrameShape(QtWidgets.QFrame.Box)
w_in_tab[i].setFrameShadow(QtWidgets.QFrame.Raised)
w_in_tab[i].setLineWidth(1)
w_in_tab[i].setMidLineWidth(0)
w_in_tab[i].setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
w_in_tab[i].setIndent(3)
w_in_tab[i].setObjectName(i)
if self.w.index(i)%2 ==0:
w_in_tab[i].setGeometry(QtCore.QRect(20, 180 + self.w.index(i)*25, 59, 25))
w_in_tab[i].setText(self.wt[int(self.w.index(i)/2)])
else:
w_in_tab[i].setGeometry(QtCore.QRect(140, 180 +(self.w.index(i)-1)*25, 59, 25))
scrollArea = QtWidgets.QScrollArea(parentw)
scrollArea.setEnabled(True)
scrollArea.setGeometry(QtCore.QRect(140, 7*25+180, 341, 440))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(scrollArea.sizePolicy().hasHeightForWidth())
scrollArea.setSizePolicy(sizePolicy)
scrollArea.setMinimumSize(QtCore.QSize(200, 100))
scrollArea.setWidgetResizable(True)
scrollArea.setObjectName("scrollArea")
scrollAreaWidgetContents_2 = QtWidgets.QWidget()
scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 322, 1600))
scrollAreaWidgetContents_2.setMinimumSize(QtCore.QSize(200, 1600))
scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
detail2 = QtWidgets.QTextBrowser(scrollAreaWidgetContents_2)
detail2.setGeometry(QtCore.QRect(0, 0, 321, 600))
detail2.setMinimumSize(QtCore.QSize(296, 192))
detail2.setObjectName("detail2")
scrollArea.setWidget(scrollAreaWidgetContents_2)
class Ui_MainWindow(object):
# setup main ui include calendar
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(997, 707)
MainWindow.setStyleSheet("")
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.centralWidget.setUpdatesEnabled(True)
self.calendarw = QtWidgets.QWidget(self.centralWidget)
self.calendarw.setGeometry(QtCore.QRect(560, 20, 361, 541))
self.calendarw.setObjectName("calendarw")
self.cal = QtWidgets.QCalendarWidget(self.calendarw)
self.cal.setGeometry(QtCore.QRect(0, 60, 361, 481))
self.cal.setFirstDayOfWeek(QtCore.Qt.Monday)
self.cal.setObjectName("cal")
self.cal_la = QtWidgets.QLabel(self.calendarw)
self.cal_la.setGeometry(QtCore.QRect(110, 20, 161, 31))
font = QtGui.QFont()
font.setPointSize(22)
self.cal_la.setFont(font)
self.cal_la.setAlignment(QtCore.Qt.AlignCenter)
self.cal_la.setObjectName("cal_la")
MainWindow.setCentralWidget(self.centralWidget)
MainWindow.setWindowTitle("MainWindow")
self.cal_la.setText("calendar")
#
# Ui_Tab(self.centralWidget)
# this makes me confused,I hope that when I clicked the date of the
# calendar ,then it will display the class of UI_tab.However it
# cant't work.
self.cal.clicked.connect(lambda x :Ui_Tab(self.centralWidget))
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
when I use the below code , it exactly does what I want it to do, but without clicking the calendar date.
Ui_Tab(self.centralWidget)
instead of
self.cal.clicked.connect(lambda x :Ui_Tab(self.centralWidget))
visit PyQt Window No Show
I just change the code below
self.cal.clicked.connect(lambda x :Ui_Tab(self.centralWidget))
to
self.cal.clicked.connect(Ui_Tab(self.centralWidget).show)

How to print in a QDialog the output of a function? With pyqt

I have a problem displaying the output of a function in a qdialog of pyqt.
My code is as follows:
import sys
import os
import glob
import shutil
import json
import datetime
import time
import requests
from multiprocessing import Queue
start_time = time.time()
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QPushButton, QLabel, QScrollArea, QProgressBar, QTextEdit
noreports = len(glob.glob('*.txt'))
class Dialogo(QDialog):
def __init__(self):
QDialog.__init__(self)
self.resize(400, 380)
self.nombre = QLabel(self)
self.nombre.setGeometry(QtCore.QRect(30, 20, 171, 17))
self.nombre.setObjectName("nombre")
self.label = QLabel(self)
self.label.setGeometry(QtCore.QRect(30, 50, 121, 17))
self.label.setObjectName("label")
self.pushButton = QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(260, 310, 86, 28))
self.pushButton.setObjectName("pushButton")
self.scrollArea = QScrollArea(self)
self.scrollArea.setGeometry(QtCore.QRect(30, 120, 341, 171))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 337, 167))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.progressBar = QProgressBar(self)
self.progressBar.setGeometry(QtCore.QRect(30, 80, 341, 23))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.textEdit = QTextEdit(self)
self.scrollArea.setWidget(self.textEdit)
self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self, Dialogo):
global noreports
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("Dialog", "Process..."))
self.label.setText(_translate("Dialog", "Files:" + str(noreports)))
self.pushButton.setText(_translate("Dialog", "Terminate"))
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(321, 247)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../../../../api-server/favicon.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
Form.setWindowOpacity(0.98)
Form.setProperty("sunrise", QtGui.QPixmap("../../../../api-server/ejemplo.png"))
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 130, 281, 51))
self.lineEdit.setAutoFillBackground(True)
self.lineEdit.setObjectName("lineEdit")
self.DescriptLbl = QtWidgets.QLabel(Form)
self.DescriptLbl.setGeometry(QtCore.QRect(20, 100, 281, 20))
self.DescriptLbl.setObjectName("DescriptLbl")
self.showinformBtn = QtWidgets.QPushButton(Form)
self.showinformBtn.setGeometry(QtCore.QRect(30, 190, 161, 31))
self.showinformBtn.setStyleSheet("background:rgb(110, 175, 255);\n"
"color:rgb(36, 36, 36);\n"
"border-radius:3px;\n"
"border-color:black;")
self.showinformBtn.setObjectName("showinformBtn")
self.dialogo = Dialogo()
self.showinformBtn.clicked.connect(self.abrirDialogo)
#self.showinformBtn.clicked.connect(self.showinform)
self.CancelarBtn = QtWidgets.QPushButton(Form)
self.CancelarBtn.setGeometry(QtCore.QRect(220, 190, 75, 31))
self.CancelarBtn.setStyleSheet("background:rgb(226, 76, 31);\n"
"color:rgb(36, 36, 36);\n"
"border-radius:3px;\n"
"border-color:black;")
self.CancelarBtn.setObjectName("CancelarBtn")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(20, 220, 311, 20))
self.label_2.setTextFormat(QtCore.Qt.RichText)
self.label_2.setScaledContents(True)
self.label_2.setOpenExternalLinks(True)
self.label_2.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByKeyboard|QtCore.Qt.LinksAccessibleByMouse|QtCore.Qt.TextBrowserInteraction|QtCore.Qt.TextEditable|QtCore.Qt.TextEditorInteraction|QtCore.Qt.TextSelectableByKeyboard|QtCore.Qt.TextSelectableByMouse)
self.label_2.setObjectName("label_2")
self.showinformLbl = QtWidgets.QLabel(Form)
self.showinformLbl.setGeometry(QtCore.QRect(20, 30, 281, 41))
font = QtGui.QFont()
font.setPointSize(22)
self.showinformLbl.setFont(font)
self.showinformLbl.setObjectName("showinformLbl")
self.imagen = QtWidgets.QLabel(Form)
self.imagen.setGeometry(QtCore.QRect(-80, 0, 221, 231))
self.imagen.setText("")
self.imagen.setPixmap(QtGui.QPixmap("max.png"))
self.imagen.setScaledContents(False)
self.imagen.setIndent(-1)
self.imagen.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.imagen.setObjectName("imagen")
self.imagen.raise_()
self.lineEdit.raise_()
self.DescriptLbl.raise_()
self.showinformBtn.raise_()
self.CancelarBtn.raise_()
self.label_2.raise_()
self.showinformLbl.raise_()
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "api-server"))
self.lineEdit.setToolTip(_translate("Form", "<html><head/><body><p>write.</p></body></html>"))
self.lineEdit.setWhatsThis(_translate("Form", "<html><head/><body><p>write.</p></body></html>"))
self.DescriptLbl.setText(_translate("Form", "descrip inform."))
self.showinformBtn.setToolTip(_translate("Form", "<html><head/><body><p>Create report.</p></body></html>"))
self.showinformBtn.setText(_translate("Form", "show inform"))
self.CancelarBtn.setToolTip(_translate("Form", "<html><head/><body><p>Cancel.</p></body></html>"))
self.CancelarBtn.setText(_translate("Form", "Cancel"))
self.label_2.setText(_translate("Form", "<html><head/><body><p>Visit api-server. ;)</p></body></html>"))
self.showinformLbl.setText(_translate("Form", "show inform."))
def abrirDialogo(self):
nombreinform = self.lineEdit.text()
self.dialogo.nombre.setText('inform:' + nombreinform)
self.dialogo.show()
self.showinform()
def showinform(self):
current_time = datetime.datetime.today().strftime("%Y-%b-%dT%H_%M")
alle = glob.glob('*.txt')
counterfile = noreports
porcentotal = 0
suma = 100/counterfile
counterbien = 0
for file in alle:
porcentotal += float(suma)
avance = round(float(porcentotal), 0)
self.dialogo.progressBar.setProperty("value", format(avance))
self.dialogo.textEdit.setText(file + format(avance))
print ('{!r} %'.format(avance))
dict = '{!r} %'.format(avance)
f = open(file,'w')
f.write('result = ' + repr(dict) + '\n')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
The code is executed but not displayed in Dialog, only the labels but not the progress bar.
Why does this happen?
The problem is that you are using the format function badly, this is used when you have a string and you want to insert data with a certain format.
The setProperty function requires as first parameter the property you want to assign, and a second value of the type that requires the property, in your case value requires an integer value so you can pass the percentage directly without needing to change it to string.
If you read many files this can block the main thread where the GUI is drawn, to solve this you must use QApplication.processEvents()
def showinform(self):
current_time = datetime.datetime.today().strftime("%Y-%b-%dT%H_%M")
alle = glob.glob('*.txt')
counterfile = noreports
porcentotal = 0
suma = 100/counterfile
counterbien = 0
for file in alle:
porcentotal += float(suma)
avance = round(float(porcentotal), 0)
self.dialogo.progressBar.setProperty("value", avance)
self.dialogo.textEdit.append("{}{}".format(file, avance))
print ('{!r} %'.format(avance))
dict = '{!r} %'.format(avance)
QApplication.processEvents()
f = open(file,'w')
f.write('result = ' + repr(dict) + '\n')
Note: I have changed the setText function to append in QTextEdit so that it is added and not overwritten.
Instead of using setProperty you could use the setValue function of QProgressBar:
self.dialogo.progressBar.setValue(avance)
Another recommendation is to verify that the counterfile is non-zero since it could generate an exception for the division.

as the missing method of QTabWidget of this code is written?

import sys,random
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow,QDialog
class Ui_TabWidget(object):
def setupUi(self, TabWidget):
TabWidget.setObjectName("TabWidget")
TabWidget.resize(264, 113)
self.PILA = QtWidgets.QWidget()
self.PILA.setObjectName("PILA")
self.LineMOSTRAR = QtWidgets.QLineEdit(self.PILA)
self.LineMOSTRAR.setGeometry(QtCore.QRect(10, 50, 231, 21))
self.LineMOSTRAR.setObjectName("LineMOSTRAR")
self.BtnINSERTA = QtWidgets.QPushButton(self.PILA)
self.BtnINSERTA.setGeometry(QtCore.QRect(10, 10, 75, 23))
self.BtnINSERTA.setObjectName("BtnINSERTA")
self.BtnSALIR = QtWidgets.QPushButton(self.PILA)
self.BtnSALIR.setGeometry(QtCore.QRect(170, 10, 75, 23))
self.BtnSALIR.setObjectName("BtnSALIR")
self.BtnELIMINA = QtWidgets.QPushButton(self.PILA)
self.BtnELIMINA.setGeometry(QtCore.QRect(90, 10, 75, 23))
self.BtnELIMINA.setObjectName("BtnELIMINA")
my error according python is in TabWidget.addTab
TabWidget.addTab(self.PILA, "") #This is mi error according pyqt5
self.COLA = QtWidgets.QWidget()
self.COLA.setObjectName("COLA")
self.BtnINSERTA_2 = QtWidgets.QPushButton(self.COLA)
self.BtnINSERTA_2.setGeometry(QtCore.QRect(10, 10, 75, 23))
self.BtnINSERTA_2.setObjectName("BtnINSERTA_2")
self.BtnELIMINA_2 = QtWidgets.QPushButton(self.COLA)
self.BtnELIMINA_2.setGeometry(QtCore.QRect(90, 10, 75, 23))
self.BtnELIMINA_2.setObjectName("BtnELIMINA_2")
self.BtnSALIR_2 = QtWidgets.QPushButton(self.COLA)
self.BtnSALIR_2.setGeometry(QtCore.QRect(170, 10, 75, 23))
self.BtnSALIR_2.setObjectName("BtnSALIR_2")
self.LineMOSTRAR_2 = QtWidgets.QLineEdit(self.COLA)
self.LineMOSTRAR_2.setGeometry(QtCore.QRect(10, 50, 231, 21))
self.LineMOSTRAR_2.setObjectName("LineMOSTRAR_2")
TabWidget.addTab(self.COLA, "")
self.COLA_C = QtWidgets.QWidget()
self.COLA_C.setObjectName("COLA_C")
self.BtnINSERTA_3 = QtWidgets.QPushButton(self.COLA_C)
self.BtnINSERTA_3.setGeometry(QtCore.QRect(10, 10, 75, 23))
self.BtnINSERTA_3.setObjectName("BtnINSERTA_3")
self.BtnELIMINA_3 = QtWidgets.QPushButton(self.COLA_C)
self.BtnELIMINA_3.setGeometry(QtCore.QRect(90, 10, 75, 23))
self.BtnELIMINA_3.setObjectName("BtnELIMINA_3")
self.BtnSALIR_3 = QtWidgets.QPushButton(self.COLA_C)
self.BtnSALIR_3.setGeometry(QtCore.QRect(170, 10, 75, 23))
self.BtnSALIR_3.setObjectName("BtnSALIR_3")
self.LineMOSTRAR_3 = QtWidgets.QLineEdit(self.COLA_C)
self.LineMOSTRAR_3.setGeometry(QtCore.QRect(10, 50, 231, 21))
self.LineMOSTRAR_3.setObjectName("LineMOSTRAR_3")
TabWidget.addTab(self.COLA_C, "")
#WIDGET Btn
#Btn for 1st tab of mi app
self.BtnINSERTA.clicked.connect(self.INSERTA)
self.BtnELIMINA.clicked.connect(self.ELIMINA)
self.BtnSALIR.clicked.connect(self.SALIR)
#Btn for 2nd tab of mi app
self.BtnINSERTA.clicked.connect(self.INSERTA_2)
self.BtnELIMINA.clicked.connect(self.ELIMINA_2)
self.BtnSALIR.clicked.connect(self.SALIR_2)
#Btn for 3rd tab of mi app
self.BtnINSERTA.clicked.connect(self.INSERTA_3)
self.BtnELIMINA.clicked.connect(self.ELIMINA_3)
self.BtnSALIR.clicked.connect(self.SALIR_3)
self.retranslateUi(TabWidget)
TabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(TabWidget)
#Method for my btns
def INSERTA(self):
self.LineMOSTRAR.setText('PILA LLENA - DESBORDAMIENTO')
def ELIMINA(self):
self.LineMOSTRAR.setText('PILA VACIA-SUBDESBORDAMIENTO')
def SALIR(self):
exit()
def SALIR_2(self):#RESET Btn
def INSERTA_2(self):
def ELIMINA_2(self):
def INSERTA_3(self):
def ELIMINA_3(self):
def SALIR_3 (self):
exit()
#end of the method
def retranslateUi(self, TabWidget):
_translate = QtCore.QCoreApplication.translate
TabWidget.setWindowTitle(_translate("TabWidget", "TabWidget"))
self.BtnINSERTA.setText(_translate("TabWidget", "Insertar"))
self.BtnSALIR.setText(_translate("TabWidget", "SALIR"))
self.BtnELIMINA.setText(_translate("TabWidget", "Eliminar"))
TabWidget.setTabText(TabWidget.indexOf(self.PILA), _translate("TabWidget", "Tab 1"))
self.BtnINSERTA_2.setText(_translate("TabWidget", "Insertar"))
self.BtnELIMINA_2.setText(_translate("TabWidget", "Eliminar"))
self.BtnSALIR_2.setText(_translate("TabWidget", "RESET"))
TabWidget.setTabText(TabWidget.indexOf(self.COLA), _translate("TabWidget", "Tab 2"))
self.BtnINSERTA_3.setText(_translate("TabWidget", "Insertar"))
self.BtnELIMINA_3.setText(_translate("TabWidget", "Eliminar"))
self.BtnSALIR_3.setText(_translate("TabWidget", "SALIR"))
TabWidget.setTabText(TabWidget.indexOf(self.COLA_C), _translate("TabWidget", "Page"))
#Qwidget Call
app = QApplication(sys.argv)
window = QDialog()
ui = Ui_TabWidget()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
And this is my error ui.setupUi(window)
How i can fix //AttributeError: 'QDialog' object has no attribute 'addTab'
when he tried to run the code gives me errors on both lines so I can not think of anything new and do not understand that I have read many explanations
Your setupUi method is expecting a QTabWidget, but your code is passing it a QDialog. While I don't have a PyQt5 environment currently set up to test against, you should be able to simply replace your window = QDialog with a window = QTabWidget() below #Qwidget Call.

Categories

Resources