Related
I am trying to load my homescreen after the splashscreen. Upon running the code, the splashscreen manages to load but it stops there. How do I fix this?
This is my SplashScreen class
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
# layout to display splash scrren frame
layout = QVBoxLayout()
self.setLayout(layout)
# splash screen frame
self.frame = QFrame()
layout.addWidget(self.frame)
# splash screen title
self.title_label = QLabel(self.frame)
self.title_label.setObjectName('title_label')
self.title_label.resize(690, 120)
self.title_label.move(0, 5) # x, y
self.title_label.setText('Splash Screen')
self.title_label.setAlignment(Qt.AlignCenter)
# splash screen title description
self.description_label = QLabel(self.frame)
self.description_label.resize(690, 40)
self.description_label.move(0, self.title_label.height())
self.description_label.setObjectName('desc_label')
self.description_label.setText('<b>Splash Screen PyQt-5</b>')
self.description_label.setAlignment(Qt.AlignCenter)
# splash screen pogressbar
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, 180) # self.description_label.y()+130
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
# spash screen loading label
self.loading_label = QLabel(self.frame)
self.loading_label.resize(self.width() - 10, 50)
self.loading_label.move(0, self.progressBar.y() + 70)
self.loading_label.setObjectName('loading_label')
self.loading_label.setAlignment(Qt.AlignCenter)
self.loading_label.setText('Loading...')
This is the function that lets the splashscreen load and launch the WindowApp class but it does not work. What I mean by does not work I meant that it does not load the WindowApp. It just closes.
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
self.close()
MainWindow = QtWidgets.QMainWindow()
ui = WindowApp()
ui.setupUi(MainWindow)
MainWindow.show()
self.counter += 1
This is my main window
class WindowApp(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 981, 641))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.label.setFont(font)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../pygui/Homescreen.png"))
self.label.setScaledContents(True)
self.label.setWordWrap(False)
self.label.setObjectName("label")
self.infoBtn = QtWidgets.QPushButton(self.centralwidget)
self.infoBtn.setGeometry(QtCore.QRect(120, 550, 181, 41))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(15)
self.infoBtn.setFont(font)
self.infoBtn.setStyleSheet("border-radius: 10px;\n"
"border-color: 2px solid gray;\n"
"background-color: rgb(255, 255, 255);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/info_icon.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.infoBtn.setIcon(icon)
self.infoBtn.setObjectName("infoBtn")
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.startBtn.setGeometry(QtCore.QRect(310, 270, 431, 111))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(28)
self.startBtn.setFont(font)
self.startBtn.setStyleSheet("background-color: #305E6C;\n"
"color: white;\n"
"border-radius: 10px;\n"
"border: 2px solid white")
self.startBtn.setObjectName("startBtn")
self.contactsBtn = QtWidgets.QPushButton(self.centralwidget)
self.contactsBtn.setGeometry(QtCore.QRect(720, 30, 101, 101))
self.contactsBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.contactsBtn.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/typcn_contacts.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.contactsBtn.setIcon(icon1)
self.contactsBtn.setIconSize(QtCore.QSize(40, 40))
self.contactsBtn.setObjectName("contactsBtn")
self.dateBtn = QtWidgets.QPushButton(self.centralwidget)
self.dateBtn.setGeometry(QtCore.QRect(840, 30, 101, 101))
self.dateBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.dateBtn.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/Group.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.dateBtn.setIcon(icon2)
self.dateBtn.setIconSize(QtCore.QSize(40, 40))
self.dateBtn.setObjectName("dateBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 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)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.infoBtn.clicked.connect(partial(self.show_popup))
self.dateBtn.clicked.connect(self.openDateWindow)
self.startBtn.clicked.connect(self.openDriveWindow)
self.contactsBtn.clicked.connect(self.openContactWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.infoBtn.setText(_translate("MainWindow", "About"))
self.startBtn.setText(_translate("MainWindow", "Driving Mode"))
def show_popup(self):
msg = QMessageBox()
msg.setWindowTitle('About Us')
msg.setText('Testing')
msg.setIcon(QMessageBox.Information)
x = msg.exec_()
def openDateWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DateWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openDriveWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DriveWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openContactWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_ContactWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
I have tried changing this to WindowApp() just to check if the MainWindow has any problems but it manages to load without the splashscreen.
app = QApplication(sys.argv)
splash = SplashScreen()
splash.show()
sys.exit(app.exec_())
Check my answer from this previous post that I think will help you.
You can show the splash screen and when the splash screen closes then you can show your main window. It's similar to the login window from this previous question
But if you want to keep implementing your code the way you have it I think I know the problem.
You call a method which creates the QMainWindow and calls the .show() for that QMainWindow. Once the method is done the variables in that method are garbage collected and so is your QMainWindow object. The way to keep the reference and prevent those variables from getting garbage collected is by making it a class variable by adding self. in front of the variable.
I would maybe restructure a bit and do something like this:
if __name__ == "__main__":
app = QApplication(sys.argv)
splash = SplashScreen()
splash_closed = splash.exec_()
if splash_closed:
window = WindowApp()
window.show()
sys.exit(app.exec_())
Where the splashscreen is now a QDialog and when you're done loading you close the dialog with self.accept() instead of trying to show a new window:
class SplashScreen(QDialog):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
...
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
self.accept()
self.counter += 1
And you should also use the __init__ method on your main window and maybe move all the self.setupUI code in there, or just do something like this:
class WindowApp(QMainWindow):
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
Hope this helps
import sys
from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication
from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QFrame, QLabel, QProgressBar
from PyQt5.QtWidgets import QWidget
from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QTimer
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
# layout to display splash scrren frame
layout = QVBoxLayout()
self.setLayout(layout)
# splash screen frame
self.frame = QFrame()
layout.addWidget(self.frame)
# splash screen title
self.title_label = QLabel(self.frame)
self.title_label.setObjectName('title_label')
self.title_label.resize(690, 120)
self.title_label.move(0, 5) # x, y
self.title_label.setText('Splash Screen')
self.title_label.setAlignment(Qt.AlignCenter)
# splash screen title description
self.description_label = QLabel(self.frame)
self.description_label.resize(690, 40)
self.description_label.move(0, self.title_label.height())
self.description_label.setObjectName('desc_label')
self.description_label.setText('<b>Splash Screen PyQt-5</b>')
self.description_label.setAlignment(Qt.AlignCenter)
# splash screen pogressbar
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, 180) # self.description_label.y()+130
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
# spash screen loading label
self.loading_label = QLabel(self.frame)
self.loading_label.resize(self.width() - 10, 50)
self.loading_label.move(0, self.progressBar.y() + 70)
self.loading_label.setObjectName('loading_label')
self.loading_label.setAlignment(Qt.AlignCenter)
self.loading_label.setText('Loading...')
self.show()
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
'''
self.close()
MainWindow = QtWidgets.QMainWindow()
ui = WindowApp()
ui.setupUi(MainWindow)
MainWindow.show()
'''
self.ui = WindowApp()
self.ui.show()
self.close()
self.counter += 1
class WindowApp(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 981, 641))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.label.setFont(font)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../pygui/Homescreen.png"))
self.label.setScaledContents(True)
self.label.setWordWrap(False)
self.label.setObjectName("label")
self.infoBtn = QtWidgets.QPushButton(self.centralwidget)
self.infoBtn.setGeometry(QtCore.QRect(120, 550, 181, 41))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(15)
self.infoBtn.setFont(font)
self.infoBtn.setStyleSheet("border-radius: 10px;\n"
"border-color: 2px solid gray;\n"
"background-color: rgb(255, 255, 255);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/info_icon.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.infoBtn.setIcon(icon)
self.infoBtn.setObjectName("infoBtn")
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.startBtn.setGeometry(QtCore.QRect(310, 270, 431, 111))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(28)
self.startBtn.setFont(font)
self.startBtn.setStyleSheet("background-color: #305E6C;\n"
"color: white;\n"
"border-radius: 10px;\n"
"border: 2px solid white")
self.startBtn.setObjectName("startBtn")
self.contactsBtn = QtWidgets.QPushButton(self.centralwidget)
self.contactsBtn.setGeometry(QtCore.QRect(720, 30, 101, 101))
self.contactsBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.contactsBtn.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/typcn_contacts.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.contactsBtn.setIcon(icon1)
self.contactsBtn.setIconSize(QtCore.QSize(40, 40))
self.contactsBtn.setObjectName("contactsBtn")
self.dateBtn = QtWidgets.QPushButton(self.centralwidget)
self.dateBtn.setGeometry(QtCore.QRect(840, 30, 101, 101))
self.dateBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.dateBtn.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/Group.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.dateBtn.setIcon(icon2)
self.dateBtn.setIconSize(QtCore.QSize(40, 40))
self.dateBtn.setObjectName("dateBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 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)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.infoBtn.clicked.connect(partial(self.show_popup))
self.dateBtn.clicked.connect(self.openDateWindow)
self.startBtn.clicked.connect(self.openDriveWindow)
self.contactsBtn.clicked.connect(self.openContactWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.infoBtn.setText(_translate("MainWindow", "About"))
self.startBtn.setText(_translate("MainWindow", "Driving Mode"))
def show_popup(self):
msg = QMessageBox()
msg.setWindowTitle('About Us')
msg.setText('Testing')
msg.setIcon(QMessageBox.Information)
x = msg.exec_()
def openDateWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DateWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openDriveWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DriveWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openContactWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_ContactWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
app = QApplication(sys.argv)
splash = SplashScreen()
#splash.show()
sys.exit(app.exec_())
I have a question. Although some questions are similar with mine, but i cat't find how to resolve.
I create a login window, when click 'Confirm' button, hope to load a new window. But now the new window will show and close immediately.The code is below:
# -*- coding: utf-8 -*-
import sys
import time
from PySide.QtCore import *
from PySide.QtGui import *
import login
import moreFrame_modify
class show_login(QWidget,login.Ui_Form):
def __init__(self,parent=None):
super(show_login,self).__init__(parent)
self.ui = login.Ui_Form()
self.ui.setupUi(self)
def show_notepad_clk(self): #This function will trigger a new window
print "Hello world!"
# farm.destroy()
time.sleep(1)
showMemoUI().show()
def exit_login_show(self):
time.sleep(.1)
exit()
# self.destroy()
def create_new_user(self):
print "Create"
QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
class showMemoUI(QWidget,moreFrame_modify.Ui_Form):
def __init__(self,parent=None):
super(showMemoUI,self).__init__(parent)
self.ui = moreFrame_modify.Ui_Form()
print "Start show"
self.ui.setupUi(self)
print "Show notepad UI"
if __name__=='__main__':
app = QApplication(sys.argv)
farm = show_login()
farm.show()
# farm = showMemoUI()
# farm.show()
sys.exit(app.exec_())
login.py
# -- coding: utf-8 --
# Form implementation generated from reading ui file 'login.ui'
#
# Created: Mon Nov 12 17:25:34 2018
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(291, 207)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/a15.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.verticalLayout_3 = QtGui.QVBoxLayout(Form)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setSpacing(20)
self.verticalLayout_2.setContentsMargins(20, -1, 20, 20)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtGui.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.userTxt = QtGui.QLineEdit(Form)
self.userTxt.setObjectName("userTxt")
self.horizontalLayout.addWidget(self.userTxt)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtGui.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.pwdTxt = QtGui.QLineEdit(Form)
self.pwdTxt.setObjectName("pwdTxt")
self.horizontalLayout_2.addWidget(self.pwdTxt)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setSpacing(10)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.confirmBtn = QtGui.QPushButton(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.confirmBtn.setFont(font)
self.confirmBtn.setObjectName("confirmBtn")
self.horizontalLayout_3.addWidget(self.confirmBtn)
self.cancelBtn = QtGui.QPushButton(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.cancelBtn.setFont(font)
self.cancelBtn.setObjectName("cancelBtn")
self.horizontalLayout_3.addWidget(self.cancelBtn)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.regeisterBtn = QtGui.QPushButton(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.regeisterBtn.setFont(font)
self.regeisterBtn.setObjectName("regeisterBtn")
self.verticalLayout.addWidget(self.regeisterBtn)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.verticalLayout_3.addLayout(self.verticalLayout_2)
self.retranslateUi(Form)
QtCore.QObject.connect(self.confirmBtn, QtCore.SIGNAL("clicked()"), Form.show_notepad_clk)
QtCore.QObject.connect(self.cancelBtn, QtCore.SIGNAL("clicked()"), Form.exit_login_show)
QtCore.QObject.connect(self.regeisterBtn, QtCore.SIGNAL("clicked()"), Form.create_new_user)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Login", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "UserName", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Form", "Password", None, QtGui.QApplication.UnicodeUTF8))
self.confirmBtn.setText(QtGui.QApplication.translate("Form", "Confirm", None, QtGui.QApplication.UnicodeUTF8))
self.cancelBtn.setText(QtGui.QApplication.translate("Form", "Cancer", None, QtGui.QApplication.UnicodeUTF8))
self.regeisterBtn.setText(QtGui.QApplication.translate("Form", "Add a new user", None, QtGui.QApplication.UnicodeUTF8))
import myresource_rc
moreFrame_modify.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'moreFrame_modify.ui'
#
# Created: Mon Nov 12 17:25:38 2018
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(469, 458)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setWeight(75)
font.setBold(True)
Form.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/a11.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.horizontalLayout_10 = QtGui.QHBoxLayout(Form)
self.horizontalLayout_10.setObjectName("horizontalLayout_10")
self.horizontalLayout_9 = QtGui.QHBoxLayout()
self.horizontalLayout_9.setObjectName("horizontalLayout_9")
self.frame_3 = QtGui.QFrame(Form)
self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.horizontalLayout_4 = QtGui.QHBoxLayout(self.frame_3)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.dateEdit = QtGui.QDateEdit(self.frame_3)
self.dateEdit.setObjectName("dateEdit")
self.horizontalLayout_2.addWidget(self.dateEdit)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_2 = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_3.addWidget(self.label_2)
self.timeEdit = QtGui.QTimeEdit(self.frame_3)
self.timeEdit.setObjectName("timeEdit")
self.horizontalLayout_3.addWidget(self.timeEdit)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_3 = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.addrTxt = QtGui.QLineEdit(self.frame_3)
self.addrTxt.setObjectName("addrTxt")
self.horizontalLayout.addWidget(self.addrTxt)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_5 = QtGui.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label_4 = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.horizontalLayout_5.addWidget(self.label_4)
self.commTxt = QtGui.QLineEdit(self.frame_3)
self.commTxt.setObjectName("commTxt")
self.horizontalLayout_5.addWidget(self.commTxt)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.textBrowser = QtGui.QTextBrowser(self.frame_3)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout.addWidget(self.textBrowser)
self.horizontalLayout_4.addLayout(self.verticalLayout)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.pushButton_4 = QtGui.QPushButton(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.verticalLayout_2.addWidget(self.pushButton_4)
self.pushButton_5 = QtGui.QPushButton(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
self.verticalLayout_2.addWidget(self.pushButton_5)
self.pushButton_6 = QtGui.QPushButton(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.pushButton_6.setFont(font)
self.pushButton_6.setObjectName("pushButton_6")
self.verticalLayout_2.addWidget(self.pushButton_6)
spacerItem = QtGui.QSpacerItem(20, 293, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout_2.addItem(spacerItem)
self.horizontalLayout_4.addLayout(self.verticalLayout_2)
self.horizontalLayout_9.addWidget(self.frame_3)
self.horizontalLayout_10.addLayout(self.horizontalLayout_9)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Notepad", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "Date", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Form", "Time", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("Form", "Address", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("Form", "Comment", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_4.setText(QtGui.QApplication.translate("Form", "Save", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_5.setText(QtGui.QApplication.translate("Form", "Cancel", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_6.setText(QtGui.QApplication.translate("Form", "About", None, QtGui.QApplication.UnicodeUTF8))
import myresource_rc
Add login.py and moreFrame_Modify.py ,these two files are generated by QT designer.
In your case you have 2 errors:
You should not use time.sleep() in a GUI, in the case of Qt you should use QTimer since time.sleep() freezes the GUI.
A local variable created in a function only exists as long as the function is executed, in your case showMemoUI is shown and instantly eliminated, therefore the new window is not observed. The solution is to make the new window member of the class.
Using the above, the solution code is:
import sys
from PySide import QtCore, QtGui
import login
import moreFrame_modify
class show_login(QtGui.QWidget, login.Ui_Form):
def __init__(self,parent=None):
super(show_login,self).__init__(parent)
self.setupUi(self)
self.showmemoui = showMemoUI()
def show_notepad_clk(self): #This function will trigger a new window
print("Hello world!")
QtCore.QTimer.singleShot(1000, self.showmemoui.show)
def exit_login_show(self):
QtCore.QTimer.singleShot(100, QtGui.QApplication.quit)
def create_new_user(self):
print("Create")
QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
class showMemoUI(QtGui.QWidget, moreFrame_modify.Ui_Form):
def __init__(self, parent=None):
super(showMemoUI,self).__init__(parent)
print("Start show")
self.setupUi(self)
print("Show notepad UI")
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
farm = show_login()
farm.show()
sys.exit(app.exec_())
Here is the converted .ui of Qt to the .py:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_NewWindow(object):
def setupUi(self, NewWindow):
NewWindow.setObjectName(_fromUtf8("NewWindow"))
NewWindow.resize(439, 225)
self.centralwidget = QtGui.QWidget(NewWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(170, 140, 99, 27))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(40, 30, 365, 89))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_3 = QtGui.QLabel(self.widget)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout_2.addWidget(self.label_3)
self.lineEdit = QtGui.QLineEdit(self.widget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout_2.addWidget(self.lineEdit)
self.label_4 = QtGui.QLabel(self.widget)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.horizontalLayout_2.addWidget(self.label_4)
self.lineEdit_2 = QtGui.QLineEdit(self.widget)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.verticalLayout.addWidget(self.pushButton)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label = QtGui.QLabel(self.widget)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.label_2 = QtGui.QLabel(self.widget)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout.addWidget(self.label_2)
self.verticalLayout.addLayout(self.horizontalLayout)
NewWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(NewWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
NewWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(NewWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
NewWindow.setStatusBar(self.statusbar)
self.retranslateUi(NewWindow)
QtCore.QMetaObject.connectSlotsByName(NewWindow)
def retranslateUi(self, NewWindow):
NewWindow.setWindowTitle(QtGui.QApplication.translate("NewWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("NewWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("NewWindow", "A = ", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("NewWindow", "B = ", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("NewWindow", "Add", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("NewWindow", "Answer : ", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
NewWindow = QtGui.QMainWindow()
ui = Ui_NewWindow()
ui.setupUi(NewWindow)
NewWindow.show()
sys.exit(app.exec_())
After running the above python program it shows the window as:
But I have to assign the value from the line edit to the variable A and B so I can add them as:
sum = A + B
and I have show this value sum in the label Answer when we clicked on push button "Add" in above window, so please suggest proper change in above code.
You need to create a news action (ad_values in this case). Then, you need to connect this action to the Buttons signal (clicked for example). After the signal emits (i.e. the button is clicked), the actions reads the values from lineEdit and lineEdit_2, adds them up and set's the text of the label_2-widget (you might convert the values to int before [see the other answer for a simple implementation])
from PySide import QtCore, QtGui
from PySide.QtCore import Slot
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_NewWindow(object):
def setupUi(self, NewWindow):
NewWindow.setObjectName(_fromUtf8("NewWindow"))
NewWindow.resize(439, 225)
self.centralwidget = QtGui.QWidget(NewWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(170, 140, 99, 27))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(40, 30, 365, 89))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
#self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_3 = QtGui.QLabel(self.widget)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout_2.addWidget(self.label_3)
self.lineEdit = QtGui.QLineEdit(self.widget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout_2.addWidget(self.lineEdit)
self.label_4 = QtGui.QLabel(self.widget)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.horizontalLayout_2.addWidget(self.label_4)
self.lineEdit_2 = QtGui.QLineEdit(self.widget)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.verticalLayout.addWidget(self.pushButton)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label = QtGui.QLabel(self.widget)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.label_2 = QtGui.QLabel(self.widget)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout.addWidget(self.label_2)
self.verticalLayout.addLayout(self.horizontalLayout)
NewWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(NewWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
NewWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(NewWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
NewWindow.setStatusBar(self.statusbar)
self.retranslateUi(NewWindow)
QtCore.QMetaObject.connectSlotsByName(NewWindow)
self.pushButton.clicked.connect(self.add_values)
def retranslateUi(self, NewWindow):
NewWindow.setWindowTitle(QtGui.QApplication.translate("NewWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("NewWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("NewWindow", "A = ", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("NewWindow", "B = ", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("NewWindow", "Add", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("NewWindow", "Answer : ", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
#Slot()
def add_values(self):
# you should convert the values to integers for a less error-prone code
summed = float(self.lineEdit.text()) + float(self.lineEdit_2.text())
self.label_2.setText(str(summed))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
NewWindow = QtGui.QMainWindow()
ui = Ui_NewWindow()
ui.setupUi(NewWindow)
NewWindow.show()
sys.exit(app.exec_())
I´m using PySide, but it should work with PyQT as well.
You can do it like:
def sum(self):
summed = float(self.lineEdit.text()) + float(self.lineEdit_2.text())
self.label_2.setText(str(summed))
This is the file I generated using qt-designer.
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(1024, 600)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(Dialog.sizePolicy().hasHeightForWidth())
Dialog.setSizePolicy(sizePolicy)
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.gridLayout = QtGui.QGridLayout(Dialog)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.graphicsView = QtGui.QGraphicsView(Dialog)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.graphicsView.sizePolicy().hasHeightForWidth())
self.graphicsView.setSizePolicy(sizePolicy)
self.graphicsView.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
self.verticalLayout.addWidget(self.graphicsView)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 4)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Add", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.verticalLayout_2.addWidget(self.pushButton)
self.gridLayout.addLayout(self.verticalLayout_2, 1, 0, 1, 1)
self.verticalLayout_4 = QtGui.QVBoxLayout()
self.verticalLayout_4.setObjectName(_fromUtf8("verticalLayout_4"))
self.pushButton_2 = QtGui.QPushButton(Dialog)
self.pushButton_2.setText(QtGui.QApplication.translate("Dialog", "Delete", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.verticalLayout_4.addWidget(self.pushButton_2)
self.gridLayout.addLayout(self.verticalLayout_4, 1, 1, 1, 1)
self.verticalLayout_5 = QtGui.QVBoxLayout()
self.verticalLayout_5.setObjectName(_fromUtf8("verticalLayout_5"))
self.pushButton_3 = QtGui.QPushButton(Dialog)
self.pushButton_3.setText(QtGui.QApplication.translate("Dialog", "Edit", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.verticalLayout_5.addWidget(self.pushButton_3)
self.gridLayout.addLayout(self.verticalLayout_5, 1, 2, 1, 1)
self.verticalLayout_3 = QtGui.QVBoxLayout()
self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
self.label = QtGui.QLabel(Dialog)
self.label.setText(QtGui.QApplication.translate("Dialog", "Connected", None, QtGui.QApplication.UnicodeUTF8))
self.label.setObjectName(_fromUtf8("label"))
self.verticalLayout_3.addWidget(self.label)
self.gridLayout.addLayout(self.verticalLayout_3, 1, 3, 1, 1)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.tableWidget = QtGui.QTableWidget(Dialog)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.horizontalLayout.addWidget(self.tableWidget)
self.gridLayout.addLayout(self.horizontalLayout, 2, 0, 1, 3)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.tabWidget = QtGui.QTabWidget(Dialog)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.verticalLayout_6 = QtGui.QVBoxLayout(self.tab)
self.verticalLayout_6.setObjectName(_fromUtf8("verticalLayout_6"))
self.scrollArea = QtGui.QScrollArea(self.tab)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 701, 292))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.verticalLayout_7 = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout_7.setObjectName(_fromUtf8("verticalLayout_7"))
self.dockWidget = QtGui.QDockWidget(self.scrollAreaWidgetContents)
self.dockWidget.setObjectName(_fromUtf8("dockWidget"))
self.dockWidgetContents = QtGui.QWidget()
self.dockWidgetContents.setObjectName(_fromUtf8("dockWidgetContents"))
self.verticalLayout_8 = QtGui.QVBoxLayout(self.dockWidgetContents)
self.verticalLayout_8.setObjectName(_fromUtf8("verticalLayout_8"))
self.plainTextEdit = QtGui.QPlainTextEdit(self.dockWidgetContents)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.plainTextEdit.sizePolicy().hasHeightForWidth())
self.plainTextEdit.setSizePolicy(sizePolicy)
self.plainTextEdit.setObjectName(_fromUtf8("plainTextEdit"))
self.verticalLayout_8.addWidget(self.plainTextEdit)
self.dockWidget.setWidget(self.dockWidgetContents)
self.verticalLayout_7.addWidget(self.dockWidget)
self.dockWidget_2 = QtGui.QDockWidget(self.scrollAreaWidgetContents)
self.dockWidget_2.setObjectName(_fromUtf8("dockWidget_2"))
self.dockWidgetContents_2 = QtGui.QWidget()
self.dockWidgetContents_2.setObjectName(_fromUtf8("dockWidgetContents_2"))
self.verticalLayout_9 = QtGui.QVBoxLayout(self.dockWidgetContents_2)
self.verticalLayout_9.setObjectName(_fromUtf8("verticalLayout_9"))
self.plainTextEdit_2 = QtGui.QPlainTextEdit(self.dockWidgetContents_2)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.plainTextEdit_2.sizePolicy().hasHeightForWidth())
self.plainTextEdit_2.setSizePolicy(sizePolicy)
self.plainTextEdit_2.setObjectName(_fromUtf8("plainTextEdit_2"))
self.verticalLayout_9.addWidget(self.plainTextEdit_2)
self.dockWidget_2.setWidget(self.dockWidgetContents_2)
self.verticalLayout_7.addWidget(self.dockWidget_2)
self.dockWidget_3 = QtGui.QDockWidget(self.scrollAreaWidgetContents)
self.dockWidget_3.setObjectName(_fromUtf8("dockWidget_3"))
self.dockWidgetContents_3 = QtGui.QWidget()
self.dockWidgetContents_3.setObjectName(_fromUtf8("dockWidgetContents_3"))
self.verticalLayout_10 = QtGui.QVBoxLayout(self.dockWidgetContents_3)
self.verticalLayout_10.setObjectName(_fromUtf8("verticalLayout_10"))
self.plainTextEdit_3 = QtGui.QPlainTextEdit(self.dockWidgetContents_3)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.plainTextEdit_3.sizePolicy().hasHeightForWidth())
self.plainTextEdit_3.setSizePolicy(sizePolicy)
self.plainTextEdit_3.setObjectName(_fromUtf8("plainTextEdit_3"))
self.verticalLayout_10.addWidget(self.plainTextEdit_3)
self.dockWidget_3.setWidget(self.dockWidgetContents_3)
self.verticalLayout_7.addWidget(self.dockWidget_3)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayout_6.addWidget(self.scrollArea)
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
self.horizontalLayout_2.addWidget(self.tabWidget)
self.gridLayout.addLayout(self.horizontalLayout_2, 2, 3, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), QtGui.QApplication.translate("Dialog", "Tab 1", None, QtGui.QApplication.UnicodeUTF8))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), QtGui.QApplication.translate("Dialog", "Tab 2", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
The output is in the screenshot: The problem is that the Tab1 is not showing any scrollbar even I set the layout under scrollArea. The region where I am expecting a scrollbar is shown by a red rectangle.
I remember this similar question :-) How to associate a horizontal scrollbar to multiple groupbox.?
The reason you are still not getting proper scrollbars is because your widgets inside the scrollarea don't have a minimum height and are being allowed to shrink far enough down that they would never force the scrollarea to display its scrollbars.
In code, you can fix this by:
self.dockWidgetContents.setMinimumHeight(100)
Althought, you shouldn't be making any direct edits to this file being generated from Qt Designer. What you should do is go into QtDesigner, select the widgets, and at the top of their property list, set a minimum height for them.
I wanted to have a total of 8 groupbox in the Dialog box. I don't know how to associate the horizontal scrollbar so that I can scroll it down and access all the group box. In the code below I have added only 2 as an example. Any help is appreciated.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(300, 20, 81, 71))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.scrollArea = QtGui.QScrollArea(Dialog)
self.scrollArea.setGeometry(QtCore.QRect(30, 20, 251, 251))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 249, 249))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.groupBox = QtGui.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox.setGeometry(QtCore.QRect(10, 10, 211, 81))
self.groupBox.setObjectName("groupBox")
self.textEdit = QtGui.QTextEdit(self.groupBox)
self.textEdit.setGeometry(QtCore.QRect(10, 20, 171, 51))
self.textEdit.setObjectName("textEdit")
self.groupBox_2 = QtGui.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox_2.setGeometry(QtCore.QRect(10, 110, 211, 111))
self.groupBox_2.setObjectName("groupBox_2")
self.textEdit_2 = QtGui.QTextEdit(self.groupBox_2)
self.textEdit_2.setGeometry(QtCore.QRect(10, 20, 171, 84))
self.textEdit_2.setObjectName("textEdit_2")
self.verticalScrollBar = QtGui.QScrollBar(self.scrollAreaWidgetContents)
self.verticalScrollBar.setGeometry(QtCore.QRect(230, 0, 16, 241))
self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
self.verticalScrollBar.setObjectName("verticalScrollBar")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox.setTitle(QtGui.QApplication.translate("Dialog", "GroupBox", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox_2.setTitle(QtGui.QApplication.translate("Dialog", "GroupBox", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
As I said in the comments, QScrollArea doesn't need manual QScrollBar. It will create when needed. I can't be sure what your problem is without seeing the 'not working' code, but my guess is the fixed sized items and their placement. You are probably putting things outside of the widgets margin or overlapping each other so that the inner widget does not grow appropriately.
Anyway, here is a minimal example that replicates your dialog (notice the scrollbar):
import sys
from PyQt4 import QtGui, QtCore
class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
scrolllayout = QtGui.QVBoxLayout()
scrollwidget = QtGui.QWidget()
scrollwidget.setLayout(scrolllayout)
scroll = QtGui.QScrollArea()
scroll.setWidgetResizable(True) # Set to make the inner widget resize with scroll area
scroll.setWidget(scrollwidget)
self.groupboxes = [] # Keep a reference to groupboxes for later use
for i in range(8): # 8 groupboxes with textedit in them
groupbox = QtGui.QGroupBox('%d' % i)
grouplayout = QtGui.QHBoxLayout()
grouptext = QtGui.QTextEdit()
grouplayout.addWidget(grouptext)
groupbox.setLayout(grouplayout)
scrolllayout.addWidget(groupbox)
self.groupboxes.append(groupbox)
self.buttonbox = QtGui.QDialogButtonBox()
self.buttonbox.setOrientation(QtCore.Qt.Vertical)
self.buttonbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
layout = QtGui.QHBoxLayout()
layout.addWidget(scroll)
layout.addWidget(self.buttonbox)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog = MyDialog()
dialog.show()
sys.exit(app.exec_())