I am trying to embed two custom widgets into the pages of a stacked widget on a dialog page. I have mocked up my problem using the main script dialog.py which has a stacked widget with two pages promoting widget_1_UI from widget_1.py and widget_2_UI from widget_2.py to each page, respectively. It doesn't throw any errors when run but also doesn't show the content from the two modules in the stacked widget.
dialog.py
from PyQt5 import QtCore, QtGui, QtWidgets
from widget_1 import widget_1_UI
from widget_2 import widget_2_UI
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setWindowTitle("Dialog")
Dialog.resize(640, 480)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.stackedWidget = QtWidgets.QStackedWidget(Dialog)
self.page_1 = widget_1_UI()
self.stackedWidget.addWidget(self.page_1)
self.page_2 = widget_2_UI()
self.stackedWidget.addWidget(self.page_2)
self.verticalLayout.addWidget(self.stackedWidget)
self.btn_nextPage = QtWidgets.QPushButton(Dialog)
self.btn_nextPage.setText("PushButton")
self.verticalLayout.addWidget(self.btn_nextPage)
self.btn_nextPage.clicked.connect(self.nextPage)
def nextPage(self):
pageNum = self.stackedWidget.currentIndex()
if pageNum == 0:
self.stackedWidget.setCurrentIndex(1)
else:
self.stackedWidget.setCurrentIndex(0)
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_())
widget_1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class widget_1_UI(QtWidgets.QWidget):
def setupUi(self, Form):
Form.resize(614, 411)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.label = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(26)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setText("AY YO BBY GRILL")
self.verticalLayout.addWidget(self.label)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setText("Dis one do nuffin")
self.verticalLayout.addWidget(self.pushButton)
widget_2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class widget_2_UI(QtWidgets.QWidget):
def setupUi(self, Form):
Form.resize(614, 411)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.label = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(26)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setText("HOW YOU DOIN")
self.verticalLayout.addWidget(self.label)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setText("Dis do nuffin 2")
self.verticalLayout.addWidget(self.pushButton)
Figured it out! In dialog.py widget_1_UI() is set as the promoted widget for the first page of the stacked widget but there is nothing to tell it what should be inside it. Adding self.page_1.setupUi(self.page_1) does just that.
self.page_1 = widget_1_UI()
self.page_1.setupUi(self.page_1)
self.stackedWidget.addWidget(self.page_1)
The same is applied to widget_2_UI.
Related
i have a main window with a read-only QlineEdit that displays today date and a button that opens calendar widget window (from another module in the project) to choose another date ,the problem that when i choose another date the QlineEdit text in Gui do not change although the variable that stores the value changes successfully ,i searched a lot with no clue
before using QlineEdit i tried Qlabel but could not achieve the result also
here is the main window:
import sys
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
from PyQt5 import QtCore, QtGui, QtWidgets
from cal_window import prev_day, today_str, UiCalWindow
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My App")
self.setGeometry(50, 50, 800, 600)
self.label = QLineEdit(self)
self.label.setFont(QFont("Arial", 20))
self.label.setReadOnly(True)
self.label.setText(today_str)
self.setCentralWidget(self.label)
self.btn = QPushButton('open calender', self)
self.btn.move(50, 50)
self.btn.clicked.connect(self.choose_date)
def choose_date(self):
self.window = QtWidgets.QMainWindow()
self.ui = UiCalWindow()
self.ui.setupUi(self.window)
self.window.show()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
and here is the calendar sub-window:
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from datetime import date
today = date.today()
today_str = today.strftime("%Y-%m-%d")
prev_day = "2000-01-01"
class UiCalWindow(object):
def get_date(self, qDate):
global prev_day
self.day = '{0}-{1}-{2}'.format(qDate.year(), qDate.month(), qDate.day())
prev_day = self.day
print(prev_day)
def setupUi(self, cal_window):
cal_window.setObjectName("cal_window")
cal_window.setWindowModality(QtCore.Qt.ApplicationModal)
cal_window.resize(800, 600)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
cal_window.setFont(font)
self.centralwidget = QtWidgets.QWidget(cal_window)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.calendarWidget.setFont(font)
self.calendarWidget.setLayoutDirection(QtCore.Qt.RightToLeft)
self.calendarWidget.setFirstDayOfWeek(QtCore.Qt.Monday)
self.calendarWidget.setGridVisible(True)
self.calendarWidget.setVerticalHeaderFormat(QtWidgets.QCalendarWidget.NoVerticalHeader)
self.calendarWidget.setObjectName("calendarWidget")
self.gridLayout.addWidget(self.calendarWidget, 0, 0, 1, 1)
self.calendarWidget.clicked.connect(self.get_date)
self.ok_dt_btn = QtWidgets.QPushButton(self.centralwidget)
self.ok_dt_btn.setMinimumSize(QtCore.QSize(0, 50))
self.ok_dt_btn.setObjectName("ok_dt_btn")
self.ok_dt_btn.clicked.connect(cal_window.close)
self.gridLayout.addWidget(self.ok_dt_btn, 1, 0, 1, 1)
cal_window.setCentralWidget(self.centralwidget)
self.retranslateUi(cal_window)
QtCore.QMetaObject.connectSlotsByName(cal_window)
def retranslateUi(self, cal_window):
_translate = QtCore.QCoreApplication.translate
cal_window.setWindowTitle(_translate("cal_window", "MainWindow"))
self.ok_dt_btn.setText(_translate("cal_window", "go"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
cal_window = QtWidgets.QMainWindow()
ui = UiCalWindow()
ui.setupUi(cal_window)
cal_window.show()
sys.exit(app.exec_())```
You have concept problems:
Do not modify the code generated by Qt Designer (it is recommended that you restore cal_window.py.
Do not use global variables, they are unnecessary and generate more problems than solutions.
That the variable "prev_day" is updated does not imply that the text displayed by the QLabel is updated, the QLabel copies the information and does not monitor the string.
The logic is to use the signal to update the text.
import sys
from PyQt5.QtCore import pyqtSignal, QDate
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
from cal_window import UiCalWindow
class CalendarWindow(QMainWindow, UiCalWindow):
def __init__(self, parent=None):
super(CalendarWindow, self).__init__(parent)
self.setupUi(self)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My App")
self.setGeometry(50, 50, 800, 600)
self.label = QLineEdit(self)
self.label.setFont(QFont("Arial", 20))
self.label.setReadOnly(True)
self.setCentralWidget(self.label)
self.btn = QPushButton("open calender", self)
self.btn.move(50, 50)
self.cal = CalendarWindow()
self.cal.calendarWidget.clicked.connect(self.handle_date_clicked)
self.btn.clicked.connect(self.cal.show)
def handle_date_clicked(self, date):
self.label.setText(date.toString("yyyy-MM-dd"))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
I have created a GUI with PyQt5. I have total three windows. first window has one push button (**btn_OpenSecondWIndow**) which opens a second window. second window has one push button (**btn_OpenCalendar**) which opens third (Calendar) window. user picks a date from there and select a button (**btn_selecteddate**) on third window, I want to display that selection in the label (label_Date) in the second window.
Snippet to get the flow
Now I'm at this point where the first window works just fine and the second window opens BUT pushbuttons doesn't work.
The button in the second window does nothing when the window is opened from the first one but when the class "SecondWindow" is called by its own and not from the first window, it works.
here is the code:
from PyQt5 import QtWidgets
from FirstWindow import Ui_FirstWindow
from SecondWindow import Ui_SecondWindow
from Calendar import Ui_CalendarWindow
class Calendar(QtWidgets.QMainWindow, Ui_CalendarWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
class FirstWindow(QtWidgets.QMainWindow, Ui_FirstWindow):
def __init__(self):
super(FirstWindow,self).__init__()
self.setupUi(self)
self.btn_OpenSecondWIndow.clicked.connect(self.open_SecondWindow)
def open_SecondWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = SecondWindow()
self.ui.setupUi(self.window)
self.window.show()
self.setEnabled(False)
self.window.setEnabled(True)
class SecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
def __init__(self):
super(SecondWindow, self).__init__()
self.setupUi(self)
self.btn_OpenCalendar.clicked.connect(self.Open_Calendar)
def Open_Calendar(self):
self.window = Calendar()
self.window.setupUi(self.window)
self.window.show()
self.window.btn_Selecteddate.clicked.connect(self.PickedDate)
def PickedDate(self):
self.selecteddate = self.window.CalendarBox.selectedDate()
self.label_Date.setText(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
self.window.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = FirstWindow()
window.show()
sys.exit(app.exec_())
Reference Ui Class codes are as below:
First Window:
from PyQt5 import QtCore, QtWidgets
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(380, 195)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_OpenSecondWIndow = QtWidgets.QPushButton(self.centralwidget)
self.btn_OpenSecondWIndow.setGeometry(QtCore.QRect(80, 60, 221, 61))
self.btn_OpenSecondWIndow.setObjectName("btn_OpenSecondWIndow")
FirstWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(FirstWindow)
self.statusbar.setObjectName("statusbar")
FirstWindow.setStatusBar(self.statusbar)
self.retranslateUi(FirstWindow)
QtCore.QMetaObject.connectSlotsByName(FirstWindow)
def retranslateUi(self, FirstWindow):
_translate = QtCore.QCoreApplication.translate
FirstWindow.setWindowTitle(_translate("FirstWindow", "MainWindow"))
self.btn_OpenSecondWIndow.setText(_translate("FirstWindow", "Open Second Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FirstWindow = QtWidgets.QMainWindow()
ui = Ui_FirstWindow()
ui.setupUi(FirstWindow)
FirstWindow.show()
sys.exit(app.exec_())
Second WIndow:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_SecondWindow(object):
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_Date = QtWidgets.QLabel(self.centralwidget)
self.label_Date.setGeometry(QtCore.QRect(330, 60, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label_Date.setFont(font)
self.label_Date.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_Date.setObjectName("label_Date")
self.btn_OpenCalendar = QtWidgets.QPushButton(self.centralwidget)
self.btn_OpenCalendar.setGeometry(QtCore.QRect(80, 90, 191, 61))
self.btn_OpenCalendar.setObjectName("btn_OpenCalendar")
SecondWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.label_Date.setText(_translate("SecondWindow", "Date"))
self.btn_OpenCalendar.setText(_translate("SecondWindow", "Open Calendar"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SecondWindow = QtWidgets.QMainWindow()
ui = Ui_SecondWindow()
ui.setupUi(SecondWindow)
SecondWindow.show()
sys.exit(app.exec_())
Third (Calendar) Window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CalendarWindow(object):
def setupUi(self, CalendarWindow):
CalendarWindow.setObjectName("CalendarWindow")
CalendarWindow.resize(512, 458)
self.centralwidget = QtWidgets.QWidget(CalendarWindow)
self.centralwidget.setObjectName("centralwidget")
self.CalendarBox = QtWidgets.QCalendarWidget(self.centralwidget)
self.CalendarBox.setGeometry(QtCore.QRect(20, 20, 464, 289))
self.CalendarBox.setObjectName("CalendarBox")
self.btn_Selecteddate = QtWidgets.QPushButton(self.centralwidget)
self.btn_Selecteddate.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.btn_Selecteddate.setFont(font)
self.btn_Selecteddate.setObjectName("btn_Selecteddate")
CalendarWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(CalendarWindow)
self.statusbar.setObjectName("statusbar")
CalendarWindow.setStatusBar(self.statusbar)
self.retranslateUi(CalendarWindow)
QtCore.QMetaObject.connectSlotsByName(CalendarWindow)
def retranslateUi(self, CalendarWindow):
_translate = QtCore.QCoreApplication.translate
CalendarWindow.setWindowTitle(_translate("CalendarWindow", "MainWindow"))
self.btn_Selecteddate.setText(_translate("CalendarWindow", "Select"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
CalendarWindow = QtWidgets.QMainWindow()
ui = Ui_CalendarWindow()
ui.setupUi(CalendarWindow)
CalendarWindow.show()
sys.exit(app.exec_())
Thanks in Advance. :)
In FirstWindow.open_SecondWindow you don't need to create a separate QMainWindow object for the second window. SecondWindow() already returns a QMainWindow object with the proper UI set up and a slot connected to btn_OpenCalendar for opening the calendar window. Therefore it is sufficient to do something like:
def open_SecondWindow(self):
self.window = SecondWindow()
self.window.show()
self.setEnabled(False)
self.window.setEnabled(True)
I have created two widgets (QMainWindow as win_one and QDialog as win_two) with qtdesigner and PyQt5.
From win_one, I open win_two, fill-in the lineEdit and press OK to transfer the entry into a label displayed in win_one. Everything works well except two problems:
win_one window is opened as .showMaximized() but after filled-in the label, the dimension of the window changes.
the button from win_one stops to work
front_win_one.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_win_one(object):
def setupUi(self, win_one):
win_one.setObjectName("win_one")
win_one.resize(1147, 234)
self.centralwidget = QtWidgets.QWidget(win_one)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(50, 50, 111, 51))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 160, 131, 31))
self.label.setObjectName("label")
win_one.setCentralWidget(self.centralwidget)
self.retranslateUi(win_one)
QtCore.QMetaObject.connectSlotsByName(win_one)
def retranslateUi(self, win_one):
_translate = QtCore.QCoreApplication.translate
win_one.setWindowTitle(_translate("win_one", "MainWindow"))
self.pushButton.setText(_translate("win_one", "To qdialog"))
self.label.setText(_translate("win_one", "TextLabel"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win_one = QtWidgets.QMainWindow()
ui = Ui_win_one()
ui.setupUi(win_one)
win_one.show()
sys.exit(app.exec_())
front_win_two.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_win_two(object):
def setupUi(self, win_two):
win_two.setObjectName("win_two")
win_two.resize(317, 278)
self.pushButton = QtWidgets.QPushButton(win_two)
self.pushButton.setGeometry(QtCore.QRect(40, 120, 121, 23))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(win_two)
self.lineEdit.setGeometry(QtCore.QRect(30, 50, 161, 21))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(win_two)
QtCore.QMetaObject.connectSlotsByName(win_two)
def retranslateUi(self, win_two):
_translate = QtCore.QCoreApplication.translate
win_two.setWindowTitle(_translate("win_two", "Dialog"))
self.pushButton.setText(_translate("win_two", "OK"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win_two = QtWidgets.QDialog()
ui = Ui_win_two()
ui.setupUi(win_two)
win_two.show()
sys.exit(app.exec_())
back.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QDialog
from front_win_1 import Ui_win_one
from front_win_2 import Ui_win_two
class win_two(QDialog, Ui_win_two):
def __init__(self, parent=None):
super(win_two, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.vers_main)
def vers_main(self):
entry = self.lineEdit.text()
win_one().label.setText(entry)
class win_one(QMainWindow, Ui_win_one):
def __init__(self, parent=None):
super(win_one, self).__init__(parent)
self.setupUi(dialog)
self.pushButton.clicked.connect(self.open_qdialog)
def open_qdialog(self):
self.dialog_win_2 = win_two()
self.dialog_win_2.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = QMainWindow()
prog = win_one(dialog)
dialog.showMaximized()
sys.exit(app.exec_())
Thank you
Your code has some inconsistencies:
You should not do this dialog = QMainWindow(), since it is enough to create an object of the class win_one, for this you must change self.setupUi(dialog) to self.setupUi(self).
With the statement win_one().label.setText(entry) you are creating a new object, which is unnecessary, besides that you are losing the previous object so when you press the window again, QDialog is not opened, a simple solution is to pass it as parent to win_one to win_two and use the self.parent() function to access it.
All of the above is implemented in the following part:
class win_two(QDialog, Ui_win_two):
def __init__(self, parent=None):
super(win_two, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.vers_main)
def vers_main(self):
entry = self.lineEdit.text()
self.parent().label.setText(entry)
class win_one(QMainWindow, Ui_win_one):
def __init__(self, parent=None):
super(win_one, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.open_qdialog)
def open_qdialog(self):
self.dialog_win_2 = win_two(self)
self.dialog_win_2.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
prog = win_one()
prog.showMaximized()
sys.exit(app.exec_())
Note: I could never reproduce the first bug, only the second one.
I created two windows
First window
from PyQt5 import QtCore, QtGui, QtWidgets
from dial import Ui_dial
from chat import Ui_Chat
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(764, 719)
font = QtGui.QFont()
font.setKerning(False)
Form.setFont(font)
Form.setStyleSheet()
self.call_button = QtWidgets.QPushButton(Form)
self.call_button.setGeometry(QtCore.QRect(320, 500, 111, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.call_button.setFont(font)
self.call_button.setStyleSheet("")
self.call_button.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap(":/images/Images/call.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.call_button.setIcon(icon2)
self.call_button.setIconSize(QtCore.QSize(58, 58))
self.call_button.setCheckable(True)
self.call_button.setAutoExclusive(True)
self.call_button.setObjectName("call_button")
self.call_button.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Space))
self.call_button.clicked.connect(self.dialwindow)
self.ilabel = QtWidgets.QLabel(Form)
self.ilabel.setGeometry(QtCore.QRect(210, 60, 331, 191))
font = QtGui.QFont()
font.setFamily("Sylfaen")
font.setPointSize(18)
self.ilabel.setFont(font)
self.ilabel.setStyleSheet("background-image: url(:/images/Images/label_back1.jpg);")
self.ilabel.setObjectName("ilabel")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
import icons_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QDialog()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Second Window
from PyQt5 import QtCore, QtGui, QtWidgets
import dial_rc
class Ui_dial(object):
def setupUi(self, dial):
dial.setObjectName("dial")
dial.resize(739, 712)
dial.setStyleSheet()
self.dialpage_label = QtWidgets.QLabel(dial)
self.dialpage_label.setGeometry(QtCore.QRect(240, 120, 301, 511))
self.dialpage_label.setObjectName("dialpage_label")
self.callend_button = QtWidgets.QPushButton(dial)
self.callend_button.setGeometry(QtCore.QRect(340, 520, 91, 71))
self.callend_button.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/images/Images/end.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.callend_button.setIcon(icon)
self.callend_button.setIconSize(QtCore.QSize(45, 45))
self.callend_button.setObjectName("callend_button")
self.callend_button.clicked.connect(self.endcall)
def retranslateUi(self, dial):
_translate = QtCore.QCoreApplication.translate
dial.setWindowTitle(_translate("dial", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
dial = QtWidgets.QDialog()
ui = Ui_dial()
ui.setupUi(dial)
dial.show()
sys.exit(app.exec_())
How to show this different windows in single window?when click on button new window is open ,so i want show when i click on the button that second window open in same screen.so i need expertise on this problem
First modify your python script so that the class inherits from QtWidgets.QMainWindow
by
class Ui_Form(QtWidgets.Qdialog):
Then write an init function so that your class can be instantiated, i.e an object can be created. Once that is done, you can chuck out the QtWidgets.QDialog() object, the one that goes by the name dial in your script, being passed to your setupUI(). Then replace this object being passed with self.
Overall your code will look like this.
class Ui_dial(QtWidgets.Qdialog):
def __init__(self):
super().__init__()//inheriting from the object.
def setupUi(self, dial):
self.setObjectName("dial")
self.resize(739, 712)
self.setStyleSheet()
self.dialpage_label = QtWidgets.QLabel(dial)
self.dialpage_label.setGeometry(QtCore.QRect(240, 120, 301, 511))
self.dialpage_label.setObjectName("dialpage_label")
self.callend_button = QtWidgets.QPushButton(dial)
self.callend_button.setGeometry(QtCore.QRect(340, 520, 91, 71))
self.callend_button.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/images/Images/end.png"),
def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("dial", "Dialog"))
Do the same for your other script and then make objects of these classes and call them as,
First_window = Ui_Form()
First_window.show()
Second_window = Ui_dial()
Second_window.show()
You might need to use resize() to adjust the two windows and to accommodate them according to your needs.
I am trying to access all the buttons, labels, text widgets, etc inside nested QVBoxLayout and QHBoxLayout layouts using the children() method. But I am not getting the results I expect: I am only getting the layouts but not the buttons, labels, etc.
Is there a Pythonic way of iterating through all the nested layouts to obtain the buttons, labels, textedit, etc.?
Here is the part where I am having trouble:
for child in self.ui.gridLayout.children():
print "child_name:%s - type:%s" %(child.objectName(),type(child))
for grand_child in child.children():
print "gc_name:%s - type:%s" %(grand_child.objectName(),type(grand_child))
# never prints the buttons, labels, etc
for ggc in grand_child.children():
print "ggc_name:%s - type:%s" %(ggc.objectName(),type(ggc))
# never finds the QLabels
qreg = QRegExp('.+')
widgets = grand_child.findChildren(QLabel,qreg)
print widgets # prints None
Here is the whole code:
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from mygui import Ui_Form
class MyApp(QtGui.QWidget):
def __init__(self,parent=None):
super(MyApp, self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton_test.clicked.connect(self.click_button)
def click_button(self):
print "Printing layouts and widgets"
for child in self.ui.gridLayout.children():
print "child_name:%s - type:%s" %(child.objectName(),type(child))
for grand_child in child.children():
print "gc_name:%s - type:%s" %(grand_child.objectName(),type(grand_child))
# never prints
for ggc in grand_child.children():
print "ggc_name:%s - type:%s" %(ggc.objectName(),type(ggc))
qreg = QRegExp('.+')
widgets = grand_child.findChildren(QLabel,qreg)
print widgets # prints None
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyApp()
myapp.show()
sys.exit(app.exec_())
And here's mygui.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mygui.ui'
#
# Created: Thu Mar 28 14:07:12 2013
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(454, 323)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout_2.addWidget(self.label_2)
self.label = QtGui.QLabel(Form)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout_2.addWidget(self.label)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.textEdit = QtGui.QTextEdit(Form)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.verticalLayout.addWidget(self.textEdit)
self.pushButton_test = QtGui.QPushButton(Form)
self.pushButton_test.setObjectName(_fromUtf8("pushButton_test"))
self.verticalLayout.addWidget(self.pushButton_test)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Form", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_test.setText(QtGui.QApplication.translate("Form", "Print All Widgets", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
I appreciate your help and input.
Paul
I realized that all the widgets can be accessed from self. Here is the final code I used:
wtypes = [QPushButton,QLabel,QTextEdit]
qreg = QRegExp(r'.*')
mywidgets = {}
for t in wtypes:
mywidgets[t] = self.findChildren(t,qreg)
for button in mywidgets[QPushButton]:
print "button:", button.objectName()
for label in mywidgets[QLabel]:
print "label:", label.objectName()