PyQt5 DatePicker Popup - python

I am not able to make datepicker in pyqt5. I am using calendarWidget and it working fine now. But i want dropdown datepicker in my menu bar and want to show selected date in lineEdit.
I have created a layout in QDesigner and adding 'DateEdit" widget. But i want same exactly as image shown.
I searched for datepicker and getting this link :How to add Today Button in QDateEdit Pop-up QCalendarWidget
I tried many approaches but it's not working.
Note: when i run my file dateEdit widget always show the Date:[01-01-2000]. it not showing the current date.
datepicker.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame_2)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.frame = QtWidgets.QFrame(self.frame_2)
self.frame.setStyleSheet("background-color: rgb(56, 122, 179);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.frame)
self.label.setStyleSheet("color: rgb(243, 243, 243);")
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.dateEdit = QtWidgets.QDateEdit(self.frame)
self.dateEdit.setObjectName("dateEdit")
self.horizontalLayout.addWidget(self.dateEdit)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.verticalLayout_2.addWidget(self.frame)
self.tableWidget = QtWidgets.QTableWidget(self.frame_2)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.verticalLayout_2.addWidget(self.tableWidget)
self.verticalLayout.addWidget(self.frame_2)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Selected Date is :________________________"))
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_())

The QDateEdit already provides a QCalendarWidget so you only need to enable the calendarPopup property:
import sys
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.dateedit = QtWidgets.QDateEdit(calendarPopup=True)
self.menuBar().setCornerWidget(self.dateedit, QtCore.Qt.TopLeftCorner)
self.dateedit.setDateTime(QtCore.QDateTime.currentDateTime())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

For PyQt6 I had to slightly modify eyllanesc's code. Here's what worked for in in PyQt6. Changes to the original are placed below the commented out code.
import sys
from PyQt6 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.dateedit = QtWidgets.QDateEdit(calendarPopup=True)
# self.menuBar().setCornerWidget(self.dateedit, QtCore.Qt.TopLeftCorner)
self.menuBar().setCornerWidget(self.dateedit, QtCore.Qt.Corner.TopLeftCorner)
self.dateedit.setDateTime(QtCore.QDateTime.currentDateTime())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
# sys.exit(app.exec_())
sys.exit(app.exec())

Related

Passing variables between an open window and a window that hasn't been opened yet

I am currently stuck on a problem of passing variables between two scripts in a python GUI code. I think I have figured out that the variable is not getting sent from the main widget to the secondary widget but I'm not sure how to get around it.
Using designer, I've mocked up two example scripts mainWidget.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
from calledWidget import Ui_CalledWindow
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(640, 170)
Form.setWindowTitle("Form")
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("PushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton.clicked.connect(self.passString)
def passString(self):
stringToPass = self.lineEdit.text()
print(stringToPass)
openingDialog = QMessageBox()
openingDialog.setWindowTitle("Warning")
openingDialog.setText("Are sure you want to pass the string: {}?".format(stringToPass))
openingDialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
reply = openingDialog.exec()
if reply == QMessageBox.Yes:
self.stringOut = stringToPass
self.newWindow = QtWidgets.QDialog()
self.ui = Ui_CalledWindow()
self.ui.setupUi(self.newWindow)
self.newWindow.show()
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_())
and calledWidget.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CalledWindow(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(612, 178)
Dialog.setWindowTitle("Dialog")
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.listView = QtWidgets.QListWidget(Dialog)
self.listView.setObjectName("listView")
self.verticalLayout.addWidget(self.listView)
self.listView.addItem(self.stringOut)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayout.addWidget(self.buttonBox)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_CalledWindow()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
where the passString() method in mainWidget.py opens the dialog window calledWidget.py. The line self.listView.addItem(self.stringOut) in the latter should be enough to do it but I get an object attribution error instead. Any ideas?
You can get around this problem by using the shelve method given in example by https://www.tutorialspoint.com/python-object-persistence-shelve#:~:text=The%20shelve%20module%20in%20Python's,stored%20in%20a%20disk%20file.

How to insert widget into existing QGraphicsView window

I'm trying to setup a very basic GUI using PyQt5 and QtDesigner. I found a very good example with included pan and zoom function. However, I do not grasp how to get the QGraphicsView and QGraphicScene classes into the QGraphicView widget created by QtDesigner. Right now it is a separate window, not associated to any tab etc. I would much appreciate
if someone could give me some input on how to put the QGraphicsView class from the example in the existing QGraphicsView widget from QtDesigner. Thanks in advance!
From QtDesigner:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 10, 621, 491))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.graphicsView = QtWidgets.QGraphicsView(self.tab)
self.graphicsView.setGeometry(QtCore.QRect(60, 30, 361, 371))
self.graphicsView.setObjectName("graphicsView")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
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_())
From Logic:
class Logic(baseUIWidget, baseUIClass):
def __init__(self, parent=None):
super(Logic, self).__init__(parent)
self.setupUi(self)
From Pan/Zoom example:
class ZoomAndPan(QW.QGraphicsView):
def __init__(self,parent=None):
super(ZoomAndPan,self).__init__(parent)
#QW.QGraphicsView.__init__(self, parent=parent)
self.setWindowTitle('ZoomAndPan')
self.setGeometry(600,300,600,400)
# Create scene
self.sc=scene(self,self)
self.setScene(self.sc)
def mouseMoveEvent(self,event):
'Pan by manipulting sceneRect'
pos=event.pos()
pos=self.mapToScene(pos)
dx=pos.x()-self.sc.startPos.x()
dy=pos.y()-self.sc.startPos.y()
rect=self.sceneRect().getRect()
self.setSceneRect(rect[0]-dx,rect[1]-dy,rect[2],rect[3])
# Increas counter to show that the loop works
self.count+=1
self.showMatrix()
class scene(QW.QGraphicsScene):
def __init__(self,parent,myView=[]):
QW.QGraphicsScene.__init__(self,parent)
self.myView=myView
# Some items in scene
self.txt=self.addSimpleText("///////")
self.txt.setPos(2,-20)
self.txt.setScale(2)
self.txt.setBrush(QtGui.QBrush(QtCore.Qt.green))
self.addRect(0,16,20,20, pen=QtGui.QPen(QtCore.Qt.blue))
self.addRect(10,60,32,8, pen=QtGui.QPen(QtCore.Qt.red))
self.addRect(30,16,20,20, pen=QtGui.QPen(QtCore.Qt.blue))
self.N=0
def mousePressEvent(self, event):
self.myView.setDragMode(1) # Works fine without this
self.startPos=event.scenePos()
def mouseReleaseEvent(self, event):
self.myView.setDragMode(0)
def wheelEvent(self, event):
'zoom'
sc=event.delta()/100
if sc<0: sc=-1/sc
self.myView.scale(sc,sc)
self.myView.setDragMode(0)
self.myView.showMatrix()
def main():
app = QtWidgets.QApplication(sys.argv)
ui = Logic(None)
view = ZoomAndPan(ui)
ui.showMaximized()
sys.exit(app.exec_())
main()

How to make the size of a QPixmap variable depending on the size of a QFrame/QLabel

I want to program a QPixmap within a QLabel in a QFrame.
Putting the QPixmap in the label works fine so far.
But how can I set the size of the QPixmap variable so that it gets bigger/ smaller when I adjust the size of the window?
I couldn't find a phrase in the code which shows me the size of the QLabel or the QFrame...
This is my Code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(825, 668)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Sunken)
self.frame.setObjectName("frame")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.frame)
self.label.setText("")
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.horizontalLayout_2.addWidget(self.frame)
MainWindow.setCentralWidget(self.centralwidget)
self.set_pixmap()
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
def set_pixmap(self):
canvas = QtGui.QPixmap(800, 800)
canvas.fill(QtCore.Qt.white)
self.label.setPixmap(canvas)
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_())

Change QLabel text dynamically not working

I am trying to change the QLabel text dymanically using QtDesigner, pyqt5.
Below is the code i am trying to use for changing the QLabel text dymanically.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label, 0, QtCore.Qt.AlignHCenter)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:28pt;\">" + self.getTime() + "</span></p></body></html>"))
def getTime(self):
time = QTime.currentTime().toString()
return time
def data(self):
time = QTime.currentTime().toString()
print("Time: " + time)
self.label.setText(time)
return time
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ex = Ui_MainWindow()
timer = QtCore.QTimer()
timer.timeout.connect(ex.data)
timer.start(1000) # 1 Second Refesh Rate
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
When i tried to run the code in QtDesigner the output window open's for a second and closed automatically. Not sure what is causing the output window to close. Please advise me to resolve the issue.
I recommend you execute your code in the terminal/CMD since many IDEs do not handle the Qt errors, if you do it you would get the following error:
Traceback (most recent call last):
File "main.py", line 33, in data
self.label.setText(time)
AttributeError: 'Ui_MainWindow' object has no attribute 'label
The error indicates that label does not exist, and that is correct because label is created after calling setupUi() but in your case "ex" does not call it. A possible solution is to first create the window and then start the timer:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
timer = QtCore.QTimer()
timer.timeout.connect(ui.data)
timer.start(1000) # 1 Second Refesh Rate
sys.exit(app.exec_())
But a better solution is to follow the recommendations of PyQt(1) that states that you should not modify the class provided by Qt but use it as the widget interface:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label, 0, QtCore.Qt.AlignHCenter)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(
_translate(
"MainWindow",
'<html><head/><body><p><span style=" font-size:28pt;"></span></p></body></html>',
)
)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.data)
timer.start(1000)
self.data()
def data(self):
time_str = QTime.currentTime().toString()
self.label.setText(
'<html><head/><body><p><span style=" font-size:28pt;">{}</span></p></body></html>'.format(
time_str
)
)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
(1) Using the Generated Code
The problem is that you instatiate Ui_MainWindow twice: first as ex and later as ui. You connect the timer.timeout to ex, but call setupUi and show only for ui.
Try this:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
timer = QtCore.QTimer()
timer.timeout.connect(ui.data)
timer.start(1000) # 1 Second Refesh Rate
sys.exit(app.exec_())
Qt Designer is only used for designing the UI, it does not allow running the program. Qt Creator can run programs, but only "real" Qt C++ programs.
You need to launch your .py scripts outside of Qt Designer, as if it were a normal Python script.

How to passing value from main to dialog

I'm newbie for using Pyqt5.
I use qtdesigner for building GUI.
I have MainWindow for passing value to dialogwindow
I want to LineEdit( in dialogwindow) show value after user input and click button (in MainWindow)
I try
self.ui = Ui_Dialog(self,data)
but it doesn't work
My code mainpage
MainWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
class Ui_MainWindow(object):
def openDialog(self):
data = self.lineEdit.text()
self.window = QtWidgets.QDialog()
self.ui = Ui_Dialog(self,data)
self.ui.setupUi(self.window)
# MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(505, 236)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(120, 40, 91, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(290, 40, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 110, 93, 28))
self.pushButton.clicked.connect(self.openDialog)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 505, 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)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Passing Value"))
self.pushButton.setText(_translate("MainWindow", "Send"))
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_())
Dialog code.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(577, 253)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(260, 100, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(180, 100, 55, 16))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Value"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
advice me plz
Thank you,
Mint
PyQt recommends not modifying the .py generated by pyuic and Qt Designer but creating another file that uses that class to fill a widget so I recommend regenerating the MainWindow.py and dialog.py files.
Now create a main.py where we inherit the appropriate class by setting a constructor with the requirement:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
from MainWindow import Ui_MainWindow
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, text, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText(text)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.openDialog)
def openDialog(self):
data = self.lineEdit.text()
w = Dialog(data)
w.exec_()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Categories

Resources