Get ID of dynamically created button when clicked - python-3.x

I have a program that dynamically creates tabs with buttons on them, when the user clicks button, I want it to give me the button_id (number that corresponds to the tab index).
I understand that you can do something like tabwidget.currentIndex() to get index of tab being used, but I don't want that as I will eventually have a method that iterates through the number of tabs and access each button without selecting the tabs as shown below.
for i in range(1,self.tabWidget.count()):
self.tabWidget.widget(i).stagematch.click()
For example:
If user clicks 'Clear Text' button on 'Tab 2' then I want it to give me the number 2 back.
How can I accomplish this without using the currentIndex() method for the tabs
Test code:
import sys
from PyQt5 import QtCore, QtWidgets
class TabPage(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
group = QtWidgets.QGroupBox('Monty Python')
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(group)
grid = QtWidgets.QGridLayout(group)
testbutton = QtWidgets.QPushButton('Clear Text')
grid.addWidget(testbutton, 2, 2)
testbutton.clicked.connect(self.tab_match)
#testbutton.clicked.connect(self.button_id)
def button_id(self):
sender = self.sender()
print(sender.text()) # Gives text of button, i'd like a number that corresponds to the tab# that called it
def tab_match(self,button_id):
#Do something with button ID here
pass
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.tabs = QtWidgets.QTabWidget()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.tabs)
button = QtWidgets.QToolButton()
button.setToolTip('Add New Tab')
button.clicked.connect(self.addNewTab)
button.setIcon(self.style().standardIcon(
QtWidgets.QStyle.SP_DialogYesButton))
self.tabs.setCornerWidget(button, QtCore.Qt.TopRightCorner)
self.addNewTab()
def addNewTab(self):
text = 'Tab %d' % (self.tabs.count() + 1)
self.tabs.addTab(TabPage(self.tabs), text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
sys.exit(app.exec_())

Try it:
import sys
from PyQt5 import QtCore, QtWidgets
class TabPage(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent # +
self.button_id = 0 # +
group = QtWidgets.QGroupBox('Monty Python')
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(group)
grid = QtWidgets.QGridLayout(group)
testbutton = QtWidgets.QPushButton('Clear Text')
grid.addWidget(testbutton, 2, 2)
testbutton.clicked.connect(self.tab_match)
self.parent.currentChanged.connect(self.qtabwidget_currentchanged) # +
def tab_match(self):
#Do something with button ID here
print("\ndef tab_match: button_id-> {}".format(self.button_id)) # +
#QtCore.pyqtSlot(int)
def qtabwidget_currentchanged(self, index): # +
self.button_id = index
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.tabs = QtWidgets.QTabWidget()
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.tabs)
button = QtWidgets.QToolButton()
button.setToolTip('Add New Tab')
button.clicked.connect(self.addNewTab)
button.setIcon(self.style().standardIcon(
QtWidgets.QStyle.SP_DialogYesButton))
self.tabs.setCornerWidget(button, QtCore.Qt.TopRightCorner)
self.button_id = 0
self.addNewTab()
def addNewTab(self):
text = 'Tab %d' % (self.tabs.count() + 1)
self.tabs.addTab(TabPage(self.tabs), text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
sys.exit(app.exec_())

Related

How to copy selected row data from QSqlTableModel tableview into reqired Qlineedit input widgets?

Example images:
Below is my example code:
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5 import uic
import sys
import sqlite3
class UI(QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi("tableview.ui", self)
self.show()
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('book.db')
db.open()
self.model = QtSql.QSqlTableModel(self)
self.model.setTable("card")
self.model.select()
self.tableView.setModel(self.model)
#self.view_msa()
self.pushButton.clicked.connect(self.edit_items)
def edit_view(self):
self.editWindow = QtWidgets.QWidget()
self.editWindow.resize(300, 300)
self.editWindow.setWindowTitle("Edit Window")
self.editWindow.setWindowModality(Qt.ApplicationModal)
self.update = QtWidgets.QPushButton(self.editWindow)
self.update.setGeometry(QtCore.QRect(60, 150, 75, 23))
self.update.setObjectName("update")
self.widget = QtWidgets.QWidget(self.editWindow)
self.widget.setGeometry(QtCore.QRect(60, 60, 201, 74))
self.widget.setObjectName("widget")
self.formLayout = QtWidgets.QFormLayout(self.widget)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
self.name_line = QtWidgets.QLineEdit(self.widget)
self.name_line.setObjectName("name_line")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.name_line)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.age_line = QtWidgets.QLineEdit(self.widget)
self.age_line.setObjectName("age_line")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.age_line)
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.address_line = QtWidgets.QLineEdit(self.widget)
self.address_line.setObjectName("address_line")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.address_line)
self.update.setText("Update")
self.label.setText("Name")
self.label_2.setText("Age")
self.label_3.setText("Address")
self.name_line.setReadOnly(True)
self.editWindow.show()
def edit_items(self):
index = (self.tableView.selectionModel().currentIndex())
id_us = (self.tableView.model().data(index))
print(str(id_us))
self.edit_view()
app = QApplication(sys.argv)
window = UI()
app.exec_()
I am using: self.model = QtSql.QSqlTableModel, Qtableview, 3 Lineedit input widgets and 2 QPushbuttons. My SQLite3 database have 3 columns that are " name, age(int values) and gender "
My Question is:
How is it possible to do that if i select a row from tableview that 3 items should be copy into their respective lineedit input widgets as name, age and address. The name Editline is read-only, Then again if the update button is pressed, the QSqlTableModel should be updated with the reference of name lineedit(the name lineedit is read-only).
You need to access the individual indexes for each column you're interested into. This can be done by calling index.sibling or via a simple model.index(row, column) as soon as you know the current row.
Then, you can use a QDialog, which is better than a QWidget with the window modality set, as you can use its exec_() method to "block" the function until the dialog is closed.
In the following example I've connected the update button to the dialog.accept() slot, in order to update the data only when the button is clicked, otherwise if the dialog is cancelled (by pressing Esc or closing it) no change is applied.
As you can see, I didn't set any instance attribute (as you did with self.editWindow, etc), as their reference is only important within the function scope, and since you're going to recreate the dialog each time there's no use in setting them as attributes.
class UI(QMainWindow):
def __init__(self):
# ...
self.pushButton.clicked.connect(self.edit_items)
def edit_items(self):
if not self.model.rowCount():
return
index = self.tableView.currentIndex()
if index.isValid():
row = index.row()
else:
row = 0
dialog = QtWidgets.QDialog()
dialog.setWindowTitle("Edit Window")
layout = QtWidgets.QVBoxLayout(dialog)
formLayout = QtWidgets.QFormLayout()
layout.addLayout(formLayout)
name_line = QtWidgets.QLineEdit(self.model.index(row, 0).data())
formLayout.addRow('Name', name_line)
name_line.setReadOnly(True)
age_edit = QtWidgets.QSpinBox()
formLayout.addRow('Age', age_edit)
age_edit.setFocus()
age_edit.setValue(self.model.index(row, 1).data())
genders = 'M', 'F'
gender_combo = QtWidgets.QComboBox()
formLayout.addRow('Gender', gender_combo)
gender_combo.addItems(genders)
gender = self.model.index(row, 2).data()
if gender and gender.upper() in genders:
gender_combo.setCurrentIndex(genders.index(gender.upper()))
else:
gender_combo.setCurrentIndex(-1)
updateButton = QtWidgets.QPushButton('Update')
layout.addWidget(updateButton)
updateButton.clicked.connect(dialog.accept)
if not dialog.exec_():
return
self.model.setData(self.model.index(row, 1), age_edit.value(),
QtCore.Qt.EditRole)
if gender_combo.currentIndex() >= 0:
self.model.setData(self.model.index(row, 2),
gender_combo.currentText(), QtCore.Qt.EditRole)
# submit all changes to the database
self.model.submitAll()
Note: avoid overwriting existing class attributes, as you did with self.update.

How to align a label with my selection list

I am using PyQt5 to create a GUI program.
I have a problem when creating a label beside the QComboBox.
If I didnt create a label beside the QComboBox , it would look like the picture down below.
But if I added the label it would be like this :
The selection list just moved down a little bit automatically.
How can I do to make it be align to the label at the left-hand side?
(I mean just beside the CASE TYPE)
(I comment the critical part in my code)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets
import xml.etree.cElementTree as ET
class App(QMainWindow):
def __init__(self,parent=None):
super().__init__()
self.title = "Automation"
self.left = 10
self.top = 10
self.width = 400
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create textbox
self.textbox = QLineEdit(self)
self.textbox.move(80, 20)
#self.textbox.resize(50,40)
self.textbox2 = QLineEdit(self)
self.textbox2.move(80, 80)
#self.textbox2.resize(50,40)
# Create text beside editor
wid1 = QWidget(self)
self.setCentralWidget(wid1)
mytext = QFormLayout()
mytext.addRow("CASE INDEX",self.textbox)
mytext.addRow("CASE TYPE",self.textbox2)
wid1.setLayout(mytext)
#################### Critical Part #######################
self.CB = QComboBox()
self.CB.addItems(["RvR","Turntable","Fixrate"])
self.CB.currentIndexChanged.connect(self.selectionchange)
label = QLabel("CASE TYPE")
mytext.addRow(label,self.CB) # this one makes the list shift down a little bit
mytext.addWidget(self.CB)
wid1.setLayout(mytext)
##########################################################
# Create a button in the window
self.button = QPushButton('Show text', self)
self.button.move(20,150)
# connect button to function on_click
self.button.clicked.connect(self.on_click)
self.center()
self.show()
#pyqtSlot()
def on_click(self):
textboxValue = self.textbox.text()
textboxValue2 = self.textbox2.text()
QMessageBox.question(self, 'Message - pythonspot.com', "You typed: "+ textboxValue + " , second msg is: " + textboxValue2, QMessageBox.Ok, QMessageBox.Ok)
print(textboxValue)
self.textbox.setText("")
self.textbox2.setText("")
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def selectionchange(self,i):
print ("Items in the list are :")
for count in range(self.CB.count()):
print (self.CB.itemText(count))
print ("Current index",i,"selection changed ",self.CB.currentText())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Please , I need your help.
Thanks.
Try it:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets
import xml.etree.cElementTree as ET
class App(QMainWindow):
def __init__(self,parent=None):
super().__init__()
self.title = "Automation"
self.left = 10
self.top = 10
self.width = 400
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create textbox
self.textbox = QLineEdit(self)
# self.textbox.move(80, 20)
#self.textbox.resize(50,40)
self.textbox2 = QLineEdit(self)
# self.textbox2.move(80, 80)
#self.textbox2.resize(50,40)
# Create text beside editor
wid1 = QWidget(self)
self.setCentralWidget(wid1)
mytext = QFormLayout()
mytext.addRow("CASE INDEX", self.textbox)
mytext.addRow("CASE TYPE", self.textbox2)
# wid1.setLayout(mytext)
#################### Critical Part #######################
self.CB = QComboBox()
self.CB.addItems(["RvR","Turntable","Fixrate"])
self.CB.currentIndexChanged.connect(self.selectionchange)
label = QLabel("CASE TYPE")
mytext.addRow(label, self.CB) # this one makes the list shift down a little bit
# mytext.addWidget(self.CB)
# wid1.setLayout(mytext)
##########################################################
# Create a button in the window
self.button = QPushButton('Show text', self)
# self.button.move(20,150)
# connect button to function on_click
self.button.clicked.connect(self.on_click)
layoutV = QVBoxLayout(wid1) # + wid1 <<<========
layoutV.addLayout(mytext) # +
layoutV.addWidget(self.button, alignment=Qt.AlignLeft) # +
self.center()
self.show()
#pyqtSlot()
def on_click(self):
textboxValue = self.textbox.text()
textboxValue2 = self.textbox2.text()
QMessageBox.question(self, 'Message - pythonspot.com', "You typed: "+ textboxValue + " , second msg is: " + textboxValue2, QMessageBox.Ok, QMessageBox.Ok)
print(textboxValue)
self.textbox.setText("")
self.textbox2.setText("")
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def selectionchange(self,i):
print ("Items in the list are :")
for count in range(self.CB.count()):
print (self.CB.itemText(count))
print ("Current index",i,"selection changed ",self.CB.currentText())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

PyQt5 fix Qlabel position

i am new to PyQt5 and I try to create a window with grid layout for buttons. But I want to add two labels at top left and bottom right corner of the window.
from PyQt5 import QtWidgets
from PyQt5 import QtCore
import sys
class PrettyWidget(QtWidgets.QWidget):
def __init__(self):
super(PrettyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600,300, 1000, 600)
self.setWindowTitle('Program v1.0')
# Grid Layout
grid = QtWidgets.QGridLayout()
self.setLayout(grid)
self.lbl1 = QtWidgets.QLabel(self)
self.lbl1.setText('Author Information and Copy Right')
self.lbl1.adjustSize()
self.lbl1.move(588, 0)
# Label indicator
self.lbl2 = QtWidgets.QLabel(self)
self.lbl2.setText('Click import to start...')
self.lbl2.adjustSize()
self.lbl2.move(0, 0)
# Import data Button
btn1 = QtWidgets.QPushButton('Select Data', self)
btn1.resize(btn1.sizeHint())
btn1.clicked.connect(self.getData)
grid.addWidget(btn1, 0, 0)
# Import names Button
btn2 = QtWidgets.QPushButton('Select Names', self)
btn2.resize(btn2.sizeHint())
btn2.clicked.connect(self.getNames)
grid.addWidget(btn2, 0, 1)
# Run Button
btn3 = QtWidgets.QPushButton('Run', self)
btn3.resize(btn3.sizeHint())
btn3.clicked.connect(self.Run)
grid.addWidget(btn3, 1, 0)
# Save Button
btn4 = QtWidgets.QPushButton('Save',self)
btn4.resize(btn4.sizeHint())
btn4.clicked.connect(self.Save)
grid.addWidget(btn4, 1, 1)
self.show()
def getData(self):
self.lbl2.setText('Data selected!')
self.lbl2.adjustSize()
def getNames(self):
self.lbl2.setText('Names selected!')
self.lbl2.adjustSize()
def Run(self):
self.lbl2.setText('Done!')
self.lbl2.adjustSize()
def Save(self):
self.lbl2.setText('Saved!')
self.lbl2.adjustSize()
def main():
app = QtWidgets.QApplication(sys.argv)
w = PrettyWidget()
app.exec_()
if __name__ == '__main__':
main()
As you can see I use absolute position for two labels now. So when I maximize or change the window size, the label stays at the same position. How do I stick lbl1 at bottom right and lbl at top left as always?
Paste the labels into the layout.
Set the stretch factor of row row to stretch .
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
class PrettyWidget(QtWidgets.QWidget):
def __init__(self):
super(PrettyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300,100, 1000, 600)
self.setWindowTitle('Program v1.0')
# Grid Layout
grid = QtWidgets.QGridLayout()
self.setLayout(grid)
self.lbl1 = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
self.lbl1.setText('Author Information and Copy Right')
self.lbl1.adjustSize()
grid.addWidget(self.lbl1, 0, 0)
grid.setRowStretch(1, 1) # <---- Sets the stretch factor of row row to stretch .
# Label indicator
self.lbl2 = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.lbl2.setText('Click import to start...')
self.lbl2.adjustSize()
grid.addWidget(self.lbl2, 5, 1)
# Import data Button
btn1 = QtWidgets.QPushButton('Select Data', self)
btn1.resize(btn1.sizeHint())
btn1.clicked.connect(self.getData)
grid.addWidget(btn1, 2, 0)
# Import names Button
btn2 = QtWidgets.QPushButton('Select Names', self)
btn2.resize(btn2.sizeHint())
btn2.clicked.connect(self.getNames)
grid.addWidget(btn2, 2, 1)
# Run Button
btn3 = QtWidgets.QPushButton('Run', self)
btn3.resize(btn3.sizeHint())
btn3.clicked.connect(self.Run)
grid.addWidget(btn3, 3, 0)
# Save Button
btn4 = QtWidgets.QPushButton('Save',self)
btn4.resize(btn4.sizeHint())
btn4.clicked.connect(self.Save)
grid.addWidget(btn4, 3, 1)
grid.setRowStretch(4, 1) # <---- Sets the stretch factor of row row to stretch .
self.show()
def getData(self):
self.lbl2.setText('Data selected!')
self.lbl2.adjustSize()
def getNames(self):
self.lbl2.setText('Names selected!')
self.lbl2.adjustSize()
def Run(self):
self.lbl2.setText('Done!')
self.lbl2.adjustSize()
def Save(self):
self.lbl2.setText('Saved!')
self.lbl2.adjustSize()
def main():
app = QtWidgets.QApplication(sys.argv)
w = PrettyWidget()
app.exec_()
if __name__ == '__main__':
main()

how to copy the text from text edit into a string variable?

i am trying to copy the text I enter into the text edit and store it into a string variable.
I have written the following code but it shows 'python has stopped working'
from PyQt5 import QtGui,QtWidgets,QtCore
import sys
class GUI(QtWidgets.QWidget):
def __init__(self):
super(GUI,self).__init__()
self.initUI()
def initUI(self):
review = QtWidgets.QLabel('Review')
reviewEdit = QtWidgets.QTextEdit()
grid = QtWidgets.QGridLayout()
grid.addWidget(review, 3, 0)
grid.addWidget(reviewEdit, 3, 1, 5, 1)
self.setLayout(grid)
self.setGeometry(300,300,350,300)
self.setWindowTitle('Sentiment Analysis')
button = QtWidgets.QPushButton("OK")
grid.addWidget(button)
button.clicked.connect(self.copyText)
self.show()
def copyText(self):
reviewEdit.setText("text")
print(text)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
GUI = GUI()
sys.exit(app.exec_())
The program stops working because of a NameError in the copyText() method. The reviewEdit variable doesn't exit in that scope, so you can't reference it.
The way to fix this is to make all the child widgets attributes of the main class - then you can access them later using self:
class GUI(QtWidgets.QWidget):
def __init__(self):
super(GUI,self).__init__()
self.initUI()
def initUI(self):
self.review = QtWidgets.QLabel('Review')
self.reviewEdit = QtWidgets.QTextEdit()
grid = QtWidgets.QGridLayout()
grid.addWidget(self.review, 3, 0)
grid.addWidget(self.reviewEdit, 3, 1, 5, 1)
self.setLayout(grid)
self.setGeometry(300,300,350,300)
self.setWindowTitle('Sentiment Analysis')
self.button = QtWidgets.QPushButton("OK")
grid.addWidget(self.button)
self.button.clicked.connect(self.copyText)
self.show()
def copyText(self):
# self.reviewEdit.setText("text")
text = self.reviewEdit.toPlainText()
print(text)
First, you should make reviewEdit a member of GUI class. Like this:
self.reviewEdit = QtGui.QTextEdit()
next, in addBold(self), you get the text like this:
text = str(self.reviewEdit.toPlainText())

pyQt QWizard change next to finish button to exit early

Is there a way to change a next button to a finish button mid-way through a wizard if user has entered sufficient data?
The example below uses the HaveFinishButtonOnEarlyPages set - however what I'd like to do is replace the next button with a finish button if user selects a radiobutton. Is this possible?
from PyQt4 import QtGui
from PyQt4.QtGui import QWizard
import sys
def createPage1():
page = QtGui.QWizardPage()
page.setTitle("Page 1")
page.setSubTitle("Enter some data, if you don't want to enter any more data select finish early.")
essentialLabel = QtGui.QLabel("Essential data:")
essentialLineEdit = QtGui.QLineEdit()
finishEarlyRB = QtGui.QRadioButton("Select to finish wizard early")
layout = QtGui.QGridLayout()
layout.addWidget(essentialLabel, 0, 0)
layout.addWidget(essentialLineEdit, 0, 1)
layout.addWidget(finishEarlyRB, 1, 0)
page.setLayout(layout)
return page
def createPage2():
page = QtGui.QWizardPage()
page.setTitle("Page 2")
page.setSubTitle("Enter some more data.")
nonEssentialLabel = QtGui.QLabel("Non essential data:")
nonEssentialLineEdit = QtGui.QLineEdit()
layout = QtGui.QGridLayout()
layout.addWidget(nonEssentialLabel, 0, 0)
layout.addWidget(nonEssentialLineEdit, 0, 1)
page.setLayout(layout)
return page
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
wizard = QtGui.QWizard()
wizard.setOption(QWizard.HaveFinishButtonOnEarlyPages, on=True)
wizard.addPage(createPage1())
wizard.addPage(createPage2())
wizard.show()
sys.exit(wizard.exec_())
Thanks to figs I recoded this so that the user has a next and finish button. The finish button is enabled when certain conditions on the page are met as per below.
from PyQt4 import QtGui
from PyQt4.QtGui import QWizard, QWizardPage
import sys
class TrialWizard(QWizard):
def __init__(self, parent = None):
super(QWizard, self).__init__(parent)
self.setOption(QWizard.HaveFinishButtonOnEarlyPages, on=True)
self.addPage(TrialWizardPage1(self))
self.addPage(TrialWizardPage2(self))
class TrialWizardPage1(QWizardPage):
def __init__(self, parent):
super(TrialWizardPage1, self).__init__(parent)
self.setupUi()
self.connectSlots()
def setupUi(self):
self.setTitle("Page 1")
self.setSubTitle("Enter some data, if you don't want to enter any more data select finish early.")
essentialLabel = QtGui.QLabel("Essential data:")
self.essentialLineEdit = QtGui.QLineEdit()
self.finishEarlyRB = QtGui.QRadioButton("Select to finish wizard early")
layout = QtGui.QGridLayout()
layout.addWidget(essentialLabel, 0, 0)
layout.addWidget(self.essentialLineEdit, 0, 1)
layout.addWidget(self.finishEarlyRB, 1, 0)
self.setLayout(layout)
def isComplete(self):
if len(self.essentialLineEdit.text()) > 0:
return True
else:
return False
def connectSlots(self):
self.finishEarlyRB.clicked.connect(self.finishEarlyRBClicked)
def finishEarlyRBClicked(self):
if self.finishEarlyRB.isChecked():
self.setFinalPage(True)
self.completeChanged.emit()
class TrialWizardPage2(QWizardPage):
def __init__(self, parent):
super(TrialWizardPage2, self).__init__(parent)
self.setupUi()
def setupUi(self):
self.setTitle("Page 2")
self.setSubTitle("Enter some more data.")
nonEssentialLabel = QtGui.QLabel("Non essential data:")
nonEssentialLineEdit = QtGui.QLineEdit()
layout = QtGui.QGridLayout()
layout.addWidget(nonEssentialLabel, 0, 0)
layout.addWidget(nonEssentialLineEdit, 0, 1)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
wizard = TrialWizard()
wizard.show()
sys.exit(app.exec_())

Resources