Im trying to build a temperature monitor using pyqt5 on a raspberry pi 3 with ds18b20 sensors attached. I am pretty new to python and pyqt5.
The Gui file (frameless.py) was developed with QT 4 designer. The Gui should serve as a basic example for a more enviromental sensors (e.g pH EC etc.) to be implemented. The actual code (runframeless.py) is shown below.
I am well aware that is is best to use threads (or Multithreading) if more senosor data are to be read at the same time, but currently I am stuck in a pretty simple problem.
Problem:
The temperature is shown in the textlabel, but the temperature value in def read_temp(self) is not updated.
Question:
Can anyone explain why it is not updated and help me getting the code right so that the temperature value shown in the gui is changed when the temperature acually changes?
runframeless.py
# -*- coding: utf-8 -*-
# file: runframeless.py)
#-create a skeleton class(es) for Raspberry Pi GUI-
# need this
import sys
import time
import datetime
import os
import glob
#----This gets the Qt stuff------------------------
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLabel
# Import QtCreator/qtdesigner file
import frameless
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '10*')[0]
device_file = device_folder + '/w1_slave'
#-------------------------------------------------
#----class(es) for our Raspberry Pi GUI-----------
#-------------------------------------------------
class MainWindow(QMainWindow, frameless.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # gets defined in the UI file
self.label.setText(str(self.read_temp()))
self.timer = QtCore.QTimer(self)
self.timer.setInterval(1000)
self.timer.timeout.connect(self.read_temp)
self.timer.start()
def read_temp(self):
f = open(device_file, 'r')
lines = f.readlines()
f.close()
time.sleep(.1)
while lines[0].strip()[-3:] != 'YES':
time.sleep(.1)
#lines = self.read_temp()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
file: frameless.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'frameless.ui'
#
# Created: Tue Feb 27 17:31:49 2018
# by: PyQt5 UI code generator 5.3.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.setWindowFlags(Qt.FramelessWindowHint) # frameless
MainWindow.resize(800, 480)
MainWindow.setStyleSheet("background:rgb(0, 0, 0);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setStyleSheet("")
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(660, 430, 101, 31))
self.pushButton.setStyleSheet("background: rgb(255, 255, 255) ")
self.pushButton.setObjectName("pushButton")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 170, 751, 41))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setStyleSheet("background: rgb(255, 255, 255) ")
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.label = QtWidgets.QLabel(self.widget)
self.label.setStyleSheet("background: rgb(255, 255, 255) ")
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "exit"))
self.label_2.setText(_translate("MainWindow", "Ds18B20-sensor"))
self.label.setText(_translate("MainWindow", "TextLabel"))
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 debugging code, it pays to logically step through how the program works.
Firstly, this line self.label.setText(str(self.read_temp())) runs once as it is in the __init__ method. This calls the self.read_temp() method which returns the temperature and places it in the text box.
Next, you create a QTimer in the __init__ method, which calls self.read_temp() every second. So when the timer triggers every second, self.read_temp is run, the temperature is read out, and returned to whatever internal code the QTimer uses to call your method.
At this point the QTimer throws away the return value because it doesn't want it, can't use it, etc.
As you can probably now see, the reason why it doesn't update is because you have never told it to update.
I would suggest adding the line self.label.setText(str(temp_c)) prior to the return temp_c line in order to update the label from the code that runs every second.
I made a repository in github that contain a code in python and PyQt5 for temperature control (monitoring) whith raspberry Pi and ds18b20 sensor
You can use that by following link
https://github.com/salarshekari/ds18b20-raspberry-pyqt5
please do not forget star if you like that
Related
Good afternoon. I'm trying to run an application in a thread that will run a long process, but so that I have control over this process so that I can interrupt the application on a button and then continue. Simulation of the application while just reading the save file (left for example), however, the application crashes in the same version of its writing.
Here is my appearance file:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'StartForm.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 643)
MainWindow.setMinimumSize(QtCore.QSize(400, 500))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton_Update_BFO = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Update_BFO.setMinimumSize(QtCore.QSize(0, 30))
self.pushButton_Update_BFO.setCheckable(True)
self.pushButton_Update_BFO.setObjectName("pushButton_Update_BFO")
self.verticalLayout.addWidget(self.pushButton_Update_BFO)
self.plainTextEditLogger = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEditLogger.setStyleSheet("QPlainTextEdit\n"
"{\n"
" background-color: palette(base);\n"
" border-style:solid;\n"
" border-width: 1px;\n"
" border-radius: 3px;\n"
" border-color: #D0D0D0;\n"
"}")
self.plainTextEditLogger.setObjectName("plainTextEditLogger")
self.verticalLayout.addWidget(self.plainTextEditLogger)
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.verticalLayout.addWidget(self.progressBar)
MainWindow.setCentralWidget(self.centralwidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 22))
self.menuBar.setObjectName("menuBar")
self.menu = QtWidgets.QMenu(self.menuBar)
self.menu.setObjectName("menu")
MainWindow.setMenuBar(self.menuBar)
self.menuBar.addAction(self.menu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_Update_BFO.setText(_translate("MainWindow", "Обновить список ссылок на bfo.nalog.ru"))
self.menu.setTitle(_translate("MainWindow", "Настройки"))
Here is my main file:
import sys
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QCoreApplication
import StartForm
import FormClasses
import need_Classes
def main():
app = QtWidgets.QApplication(sys.argv)
window = FormClasses.ExampleApp()
window.show()
app.exec_()
if __name__ == '__main__':
main()
And here is the problematic part:
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QCoreApplication
import StartForm
import main
import sys
import time
import threading
from threading import Lock
import datetime
from dateutil.parser import parse
from datetime import date#, datetime
import urllib3.contrib.pyopenssl
import main
import need_Classes
class ExampleApp(QtWidgets.QMainWindow, StartForm.Ui_MainWindow):
def __init__(self):
super().__init__()
self.count=0
self.setupUi(self)
self.pushButton_Update_BFO.clicked.connect(self.start_update_Update_BFO)
self.progressBar.setMinimum(1)
self.progressBar.setValue(1)
def main_update_Update_BFO(self):
#urllib3.contrib.pyopenssl.inject_into_urllib3()
self.plainTextEditLogger.clear()
self.progressBar.setValue(1)
try:
with open('save_update_Update_BFO.json', 'w', encoding="utf-8") as save_f:
self.save =need_Classes.save_update_Update_BFO(save_f)
save_f.close()
print("Retrieve recent saves\nSave successfully received")
except Exception as exc:
self.print("Retrieving saves:\nError reading saves!" + str(exc))
return
print("Hi!")
def ticker(self, event):
while event.is_set():
self.main_update_Update_BFO()
time.sleep(1)
def start_update_Update_BFO(self):
if self.pushButton_Update_BFO.isChecked():
self.event = threading.Event()
self.event.set()
self.t1 = threading.Thread(target=self.ticker, args=(self.event,),)# daemon=True)
self.t1.start()
#self.t1.join()
else:
self.event.clear()
# t1.join()
Accidentally found that the problematic line is: #urllib3.contrib.pyopenssl.inject_into_urllib3()
If this line is uncommented, the program will crash.
The second question is that I can't figure out if it's necessary to use daemon=True in threads, and also, after starting the thread, if self.t1.join() should be performed
If I type these things back into when, I lose control of the form and the button.
I am newbie and maybe this is silly question.
I want to activate tab by clicking on pushButton . If I click pushButton_1 then it shows/activates tab_1, on the clicking pushButton_2 switches to tab_2, and so on . Bellow there is pic how I tried it but without success. Help me solve this and write correct code down. Thank you in advance.
This is my code :
from spex import *
import sys
class speX (Ui_MainWindow):
def __init__(self, window):
self.setupUi(window)
self.systemButton.clicked.setCurrentIndex(self, 0)
self.cpuButton.clicked.setCurrentIndex(self, 1)
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = speX (MainWindow)
MainWindow.show()
app.exec_()
I want this - when I click systemButton, activate tab indexed as 0, when click cpuButton activate tab indexed as 1.
Error provided there -
Traceback (most recent call last):
File "d:\spex\main.py", line 17, in <module>
ui = speX (MainWindow)
File "d:\spex\main.py", line 9, in __init__
self.systemButton.clicked.setCurrentIndex(self, 0)
AttributeError: 'PyQt5.QtCore.pyqtBoundSignal' object has no attribute 'setCurrentIndex'
This is example of my project - pyuic5 ui to py code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'D:/spex/example.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(763, 611)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
self.stackedWidget.setGeometry(QtCore.QRect(270, 70, 361, 361))
self.stackedWidget.setObjectName("stackedWidget")
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.buttonBox = QtWidgets.QDialogButtonBox(self.page)
self.buttonBox.setGeometry(QtCore.QRect(50, 310, 193, 28))
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.stackedWidget.addWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.commandLinkButton = QtWidgets.QCommandLinkButton(self.page_2)
self.commandLinkButton.setGeometry(QtCore.QRect(70, 190, 222, 48))
self.commandLinkButton.setObjectName("commandLinkButton")
self.stackedWidget.addWidget(self.page_2)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 110, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(50, 190, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 763, 26))
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.stackedWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.commandLinkButton.setText(_translate("MainWindow", "CommandLinkButton"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
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_())
In Qt, a signal is emitted when a particular event occurs: a button is clicked, some text is changed, etc.
Signals are then connected to slots, which are functions that can react in some way to those signals. For example, you can create a slot that changes the text in a label, and connect it to a clicked signal of a button: then, when you click on that button, the text is changed.
Why your code does not work?
Well, for instance, setCurrentIndex is a method of a QStackedWidget (similarly to a QTabWidget), so the following doesn't make much sense:
self.systemButton.clicked.setCurrentIndex(self, 0)
Where is the reference to the stacked widget?
In PyQt, you can connect signals and slots like this:
someObject.signal.connect(someFunction)
Note that someFunction is a reference to that function; the following will not work:
someObject.signal.connect(someFunction())
If you want to change the index of a stacked widget after clicking on a button, you have to create a function that changes that index, and connect it to the clicked signal.
class speX (Ui_MainWindow):
def __init__(self, window):
self.setupUi(window)
self.pushButton.clicked.connect(self.goToSecondPage)
def goToSecondPage(self):
self.stackedWidget.setCurrentIndex(1)
I strongly suggest you to carefully study how python classes work and, generally speaking, the Object Oriented programming paradigm.
The simplist solution would be to connect your buttons to a function that changes the tab index on click
tabs = QtWidget.QTabWidget()
tab1 = QtWidget.QWidget()
tabs.addTab(self.tab1, "Tab 1")
systemButton.clicked.connect(tabIndex0)
def tabIndex0():
tabs.setCurrentIndex(0)
I use the pyqt5.I connect the pushbutton signal to the dididi(). It is supported when I clicked the button it will print the message, but when I click the button, it does not print the message. Why? What can I do to solve it?
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'bank1.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QBasicTimer
from PyQt5.QtGui import QColor, QFontMetrics, QPainter, QPalette
from PyQt5.QtWidgets import (QApplication, QDialog, QLineEdit, QVBoxLayout,
QWidget)
from PyQt5.QtCore import QCoreApplication
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(80, 200, 71, 21))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(230, 200, 72, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(140, 90, 104, 31))
self.textEdit.setObjectName("textEdit")
self.pushButton.clicked.connect(self.dididi)
self.pushButton_2.clicked.connect(self.dididi)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "RCT"))
self.pushButton.setText(_translate("Form", "连接"))
self.pushButton_2.setText(_translate("Form", "发送"))
def dididi(self):
print("hello world")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
widget = QWidget(None)
Ui_Form().setupUi(widget)
widget.show()
sys.exit(app.exec_())
pass
Just put #staticmethod above your "dididi()" method.
See explanation about #staticmethod #classmethod and normal methods.
#staticmethod
def dididi():
print("hello world")
If you don't want to loose the context (the self) then you could do it this way:
Instead of directly connecting the clicked event to your method, in tour setupUi you create an internal function and you connect the clicked event to it. This would do the trick as you still have access to the self object at this context:
def clicked():
self.dididi()
self.pushButton.clicked.connect(clicked)
This way, you can still define your dididi function as a normal method and use local variables of your class via the self argument.
I have created short application with Pyqt5 designer:
Here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(752, 674)
self.formLayout_2 = QtWidgets.QFormLayout(Form)
self.formLayout_2.setObjectName("formLayout_2")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.formLayout_2.setLayout(0, QtWidgets.QFormLayout.LabelRole, self.formLayout)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.pushButton)
self.widget = QtWidgets.QWidget(Form)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setStyleSheet("background-color:white;")
self.widget.setObjectName("widget")
self.formLayout_2.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.widget)
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.pushButton_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Letters"))
self.pushButton_2.setText(_translate("Form", "Numbers"))
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_())
This script is connected to appexe script(dialog):
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import string
from functools import partial
from setup_test import Ui_Form
from PyQt5.QtWidgets import QWidget, QGridLayout, QPushButton, QApplication
class myprog(Ui_Form):
def __init__ (self, dialog):
Ui_Form.__init__(self)
self.setupUi(dialog)
self.symbols = [string.ascii_lowercase, string.digits]
self.buttons = [self.pushButton, self.pushButton_2]
for i,v in enumerate(self.buttons):
self.buttons[i].clicked.connect(partial(self.application, i))
def application(self, i):
self.grid1 = QGridLayout(self.widget)
names = [x for x in self.symbols[i]]
positions = [(j,d) for j in range(7) for d in range(6)]
for a,b in zip(positions, names):
button = QPushButton(b)
self.grid1.addWidget(button, *a)
print(self.grid1.count())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
appexe = myprog(dialog)
dialog.show()
sys.exit(app.exec_())
I am new to Pyqt5. The basic idea is: whenever you click the button 'Letters' or button 'Numbers', the widget below should layout all the sysmbols within Letters or buttons.
If you do execute the appexe script, you can see, it works for the first try. (Please ignore the visual design). I am happy with the result. When button is pressed, it shows the buttons in grid(widget).
The problem I am facing is, how to clear the grid and it's widget when other button is hit > and display the new grid, new widgets.
If you look close, there is also a print statement:
print(self.grid1.count())
When I am in application and i am clicking letter and number button, it tells me how many widgets are within grid, however I just can't find a way how to display the new setup.
I would appreciate your help and your ideas. Thank you.
There are a number of different ways to remove a layout and its child widgets. However, the only one that seems to work properly with your particular example, is this one:
def application(self, i):
layout = self.widget.layout()
if layout is not None:
QWidget().setLayout(layout)
...
This works because setting the layout on a different widget will automatically re-parent that layout and all its children. Then when the temporary parent widget is immediately garbage-collected, Qt will automatically delete all its children as well.
This may look like a long question but it is actually really short, I've just decided to copy all the working code here.
I have a main window and a Tip of the Day widget.
I generated both the UI using the PyQt Designer.
I can open the Tip of the Day widget from the main window menu but I'm not able to make the buttons work:
I'd like to replace some text in the Tip of the Day widget when the previous and the next buttons are clicked.
I have the following main window called MainWindow.py:
from PyQt4 import QtCore, QtGui
from MainWindowUi import Ui_MainWindow
from FormUi import Ui_Form
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
# Main window user interface elements
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Main window signal/slot connections
self.setupConnections()
#QtCore.pyqtSlot()
def showTipDialog(self):
'''Trig dialog Tip'''
form = QtGui.QDialog()
form.ui = Ui_Form()
form.ui.setupUi(form)
form.exec_()
def setupConnections(self):
'''Signal and Slot Support'''
self.connect(self.ui.actionTip_of_the_Day, QtCore.SIGNAL('triggered()'), self.showTipDialog)
I have the following main.py:
import sys
from PyQt4 import QtGui
from MainWindow import MainWindow
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I have the following main window UI called MainWindowUi.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created: Thu May 21 20:26:31 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuHelp = QtGui.QMenu(self.menubar)
self.menuHelp.setObjectName(_fromUtf8("menuHelp"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionTip_of_the_Day = QtGui.QAction(MainWindow)
self.actionTip_of_the_Day.setObjectName(_fromUtf8("actionTip_of_the_Day"))
self.menuHelp.addAction(self.actionTip_of_the_Day)
self.menubar.addAction(self.menuHelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.menuHelp.setTitle(_translate("MainWindow", "Help", None))
self.actionTip_of_the_Day.setText(_translate("MainWindow", "Tip of the Day", None))
I have the following widget form UI FormUi.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Form.ui'
#
# Created: Thu May 21 23:57:41 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(418, 249)
self.verticalLayout = QtGui.QVBoxLayout(Form)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton_previous = QtGui.QPushButton(Form)
self.pushButton_previous.setObjectName(_fromUtf8("pushButton_previous"))
self.horizontalLayout.addWidget(self.pushButton_previous)
self.pushButton_next = QtGui.QPushButton(Form)
self.pushButton_next.setObjectName(_fromUtf8("pushButton_next"))
self.horizontalLayout.addWidget(self.pushButton_next)
self.verticalLayout.addLayout(self.horizontalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.lineEdit.setText(_translate("Form", "Here a tip I'd like to replace by pressing the buttons below.", None))
self.pushButton_previous.setText(_translate("Form", "Previous Tip", None))
self.pushButton_next.setText(_translate("Form", "Next Tip", None))
Please run main.py in order to open the main window and click Help > Tip of the Day to open the widget.
Thanks for the attention.
You have a MainWindow class (which you instantiate from the main() function) which you have written to instantiate your Ui_MainWindow class (thus creating the GUI) and link a button to a method which pops up the dialog.
Now just apply the same logic to the dialog. Instead of creating a QDialog() directly in showTipDialog, instead instantiate a subclass QDialog. Write the subclass in a similar way to what you've done for MainWindow. Connect the clicked signals from the prev/next pushbuttons to appropriate methods (which you write) that change the contents of the QLineEdit.