Very much a beginner on the UI side of things!
I'm trying to transfer a list from a dialog to the main window (when a button is pressed) and then transfer a new list back to the window when the dialog is accepted.
Here's a MWE of what I'm working on to give you an idea.
It should print out the following,
Desired output:
[b]
.
[b]
[a]
.
[a]
Achieved output:
[b]
.
[b]
[a]
.
[b]
Thanks in advance!
MWE:
import sys
from PyQt4 import QtCore, QtGui#pyqt5, QtWidgets
from Scrabble_gui import Ui_MainWindow
from Input_letters_gui import Ui_Dialog as Popup
class ScrabbleGuiProgram(Ui_MainWindow):
def __init__(self, dialog):
Ui_MainWindow.__init__(self)
self.setupUi(dialog)
self.In_letters.clicked.connect(self.Input_letters)
def Input_letters(self):
"""Prints "b" and opens pop up.
after popup is closed, prints "a"
"""
letter_list = ["b"]
#app = QtGui.QApplication(sys.argv)
print(letter_list)
print(".")
PUDialog = QtGui.QDialog()
pop = Input_letters_popup(PUDialog, letter_list)
PUDialog.exec_()
print(".")
print(letter_list)
class Input_letters_popup(Popup):
def __init__(self, dialog, letter_list):
"""when open prints incoming list ("b")
"""
print(letter_list)
Popup.__init__(self)
self.setupUi(dialog)
self.buttonBox.accepted.connect(self.Get_letters)#lambda: self.Get_letters)
#self.buttonBox.rejected.connect()
def Get_letters(self):
"""on popup exit prints new list ("a")
"""
letters = ["a"]
print(letters)
return letters
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QMainWindow()
prog = ScrabbleGuiProgram(Dialog)#Input_letters_popup(Dialog)
Dialog.show()
sys.exit(app.exec_())
Scrabble_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Scrabble.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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(406, 267)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Gen_words = QtGui.QPushButton(self.centralwidget)
self.Gen_words.setGeometry(QtCore.QRect(250, 20, 121, 28))
self.Gen_words.setObjectName(_fromUtf8("Gen_words"))
self.Long_word = QtGui.QLineEdit(self.centralwidget)
self.Long_word.setGeometry(QtCore.QRect(229, 100, 141, 22))
self.Long_word.setReadOnly(True)
self.Long_word.setObjectName(_fromUtf8("Long_word"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(279, 180, 41, 21))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(99, 143, 131, 21))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.High_score = QtGui.QLineEdit(self.centralwidget)
self.High_score.setGeometry(QtCore.QRect(319, 180, 51, 22))
self.High_score.setReadOnly(True)
self.High_score.setObjectName(_fromUtf8("High_score"))
self.High_word = QtGui.QLineEdit(self.centralwidget)
self.High_word.setGeometry(QtCore.QRect(229, 143, 141, 22))
self.High_word.setReadOnly(True)
self.High_word.setObjectName(_fromUtf8("High_word"))
self.In_letters = QtGui.QPushButton(self.centralwidget)
self.In_letters.setGeometry(QtCore.QRect(90, 57, 121, 28))
self.In_letters.setObjectName(_fromUtf8("In_letters"))
self.Gen_letters = QtGui.QPushButton(self.centralwidget)
self.Gen_letters.setGeometry(QtCore.QRect(90, 20, 121, 28))
self.Gen_letters.setObjectName(_fromUtf8("Gen_letters"))
self.letters = QtGui.QListWidget(self.centralwidget)
self.letters.setGeometry(QtCore.QRect(20, 50, 41, 161))
self.letters.setObjectName(_fromUtf8("letters"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(145, 100, 81, 20))
self.label.setObjectName(_fromUtf8("label"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 20, 41, 21))
self.label_4.setObjectName(_fromUtf8("label_4"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 406, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Descrabbler", None))
self.Gen_words.setText(_translate("MainWindow", "Best Words", None))
self.label_2.setText(_translate("MainWindow", "Score", None))
self.label_3.setText(_translate("MainWindow", "Highest Scoring Word", None))
self.In_letters.setText(_translate("MainWindow", "Input Letters", None))
self.Gen_letters.setText(_translate("MainWindow", "Generate Letters", None))
self.label.setText(_translate("MainWindow", "Longest Word", None))
self.label_4.setText(_translate("MainWindow", "Letters", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Input_letters_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Input_letters.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 173)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 90, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.ltr1 = QtGui.QLineEdit(Dialog)
self.ltr1.setGeometry(QtCore.QRect(20, 40, 31, 22))
self.ltr1.setObjectName(_fromUtf8("ltr1"))
self.ltr2 = QtGui.QLineEdit(Dialog)
self.ltr2.setGeometry(QtCore.QRect(60, 40, 31, 22))
self.ltr2.setObjectName(_fromUtf8("ltr2"))
self.ltr3 = QtGui.QLineEdit(Dialog)
self.ltr3.setGeometry(QtCore.QRect(100, 40, 31, 22))
self.ltr3.setObjectName(_fromUtf8("ltr3"))
self.ltr4 = QtGui.QLineEdit(Dialog)
self.ltr4.setGeometry(QtCore.QRect(140, 40, 31, 22))
self.ltr4.setObjectName(_fromUtf8("ltr4"))
self.ltr5 = QtGui.QLineEdit(Dialog)
self.ltr5.setGeometry(QtCore.QRect(180, 40, 31, 22))
self.ltr5.setObjectName(_fromUtf8("ltr5"))
self.ltr6 = QtGui.QLineEdit(Dialog)
self.ltr6.setGeometry(QtCore.QRect(220, 40, 31, 22))
self.ltr6.setObjectName(_fromUtf8("ltr6"))
self.ltr7 = QtGui.QLineEdit(Dialog)
self.ltr7.setGeometry(QtCore.QRect(260, 40, 31, 22))
self.ltr7.setObjectName(_fromUtf8("ltr7"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Input Letters", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Related
My python/QT (Pyqt4) program is supposed to output a person's age after the calculate button has been pressed, but it doesnt, where have I gone wrong. My code and screenshot of the program when running is hereby attached.
my python code that does not display the text in a label.
this is my .pyw file
import sys
from ass2q2 import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.calculateBtn, QtCore.SIGNAL('clicked()'),self.dispmessage)
def dispmessage(self):
bd=(self.ui.calBD.selectedDate())
curr= QDate.currentDate()
yourAge = bd.daysTo(curr)
self.ui.labelOutput.setText("Your Age (in days) is: " +yourAge.toString())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
ass2q2.py file
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ass2q2.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(705, 531)
self.calBD = QtGui.QCalendarWidget(Dialog)
self.calBD.setGeometry(QtCore.QRect(40, 150, 280, 155))
self.calBD.setObjectName(_fromUtf8("calBD"))
self.calCurr = QtGui.QCalendarWidget(Dialog)
self.calCurr.setEnabled(False)
self.calCurr.setGeometry(QtCore.QRect(370, 150, 280, 155))
self.calCurr.setObjectName(_fromUtf8("calCurr"))
self.labelbirth = QtGui.QLabel(Dialog)
self.labelbirth.setGeometry(QtCore.QRect(40, 120, 271, 16))
self.labelbirth.setObjectName(_fromUtf8("labelbirth"))
self.labelToDate = QtGui.QLabel(Dialog)
self.labelToDate.setGeometry(QtCore.QRect(380, 120, 261, 21))
self.labelToDate.setObjectName(_fromUtf8("labelToDate"))
self.labelOutput = QtGui.QLabel(Dialog)
self.labelOutput.setGeometry(QtCore.QRect(60, 340, 581, 41))
self.labelOutput.setLineWidth(3)
self.labelOutput.setText(_fromUtf8(""))
self.labelOutput.setObjectName(_fromUtf8("labelOutput"))
self.calculateBtn = QtGui.QPushButton(Dialog)
self.calculateBtn.setGeometry(QtCore.QRect(290, 460, 141, 51))
self.calculateBtn.setObjectName(_fromUtf8("calculateBtn"))
self.labelApp = QtGui.QLabel(Dialog)
self.labelApp.setGeometry(QtCore.QRect(140, 60, 411, 41))
self.labelApp.setObjectName(_fromUtf8("labelApp"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.labelbirth.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-weight:600;\">Select the Birth Date</span></p></body></html>", None))
self.labelToDate.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-weight:600;\">Current Date</span></p></body></html>", None))
self.calculateBtn.setText(_translate("Dialog", "C A L C U L A T E", None))
self.labelApp.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">AGE (in days) CALCULATOR</span></p></body></html>", None))
Reading data in from a simple dict d={key:['desc',dateadded]}
Goal is to add data into table view and sort by date in the list. Cannot get any data to add when trying to put table on main window.
The issue lies with how I am placing the table on the main window. I have a working widget with table and am now trying to combine the two files. Currently the application crashes. Dont want you to write the code but a nudge in the right direction would be a big help.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'display.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4.QtGui import (QMainWindow, QApplication)
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
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 __init__(self):
#app = QApplication(sys.argv)
self.window = QMainWindow()
self.setupUi(self.window)
self.window.show()
self.setmydata()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(296, 478)
MainWindow.setIconSize(QtCore.QSize(0, 0))
MainWindow.setAnimated(False)
MainWindow.setTabShape(QtGui.QTabWidget.Rounded)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableView = QtGui.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(20, 50, 261, 391))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableView.sizePolicy().hasHeightForWidth())
self.tableView.setSizePolicy(sizePolicy)
self.tableView.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers)
self.tableView.setDragEnabled(True)
self.tableView.setAlternatingRowColors(True)
self.tableView.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
self.tableView.setSortingEnabled(False)
self.tableView.setObjectName(_fromUtf8("tableView"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 10, 121, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(160, 10, 121, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 296, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def setmydata(self):
d={"12345678":["do some stuff here that needs to be in a large cell to wrap text","1"],"12343378":["do stuff","1"]}
self.data = {}
keys=d.keys()
self.data['col1']=keys
self.data['col2']=[]
for key in keys:
self.data['col2'].append(d[key][0])
horHeaders = []
for n, key in enumerate(sorted(self.data.keys())):
horHeaders.append(key)
for m, item in enumerate(self.data[key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Add New", None))
self.pushButton_2.setText(_translate("MainWindow", "Save", None))
app = QApplication(sys.argv)
frame = Ui_MainWindow()
#frame.show()
app.exec_()
After digging through the code I found line
self.tableView = QtGui.QTableView(self.centralwidget)
needed to be QTableWidget rather than a View. This allows for me to set values and so on.
When I press the button for go to a diferent page using setCurrentIndex I have problems with duplicate output values because if I set value "1" on QLineEdit on home_page for example and then go to conf_page and now I back to home_page, when I press the OK button for print content of QLineEdit for example, I get a duplicate output.
My code.
Project.py file.
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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(210, 191)
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(0, 0, 211, 61))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.pushButton_2 = QtGui.QPushButton(self.frame)
self.pushButton_2.setGeometry(QtCore.QRect(20, 20, 75, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self.frame)
self.pushButton_3.setGeometry(QtCore.QRect(110, 20, 75, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.stackedWidget = QtGui.QStackedWidget(Dialog)
self.stackedWidget.setGeometry(QtCore.QRect(0, 60, 211, 131))
self.stackedWidget.setObjectName(_fromUtf8("stackedWidget"))
self.page = QtGui.QWidget()
self.page.setObjectName(_fromUtf8("page"))
self.lineEdit = QtGui.QLineEdit(self.page)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.pushButton = QtGui.QPushButton(self.page)
self.pushButton.setGeometry(QtCore.QRect(70, 90, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.stackedWidget.addWidget(self.page)
self.page_2 = QtGui.QWidget()
self.page_2.setObjectName(_fromUtf8("page_2"))
self.stackedWidget.addWidget(self.page_2)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton_2.setText(_translate("Dialog", "PushButton", None))
self.pushButton_3.setText(_translate("Dialog", "PushButton", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
Project.pyw file.
from project import *
import sys
class project(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.pushButton_2, QtCore.SIGNAL('clicked()'), self.home_page)
QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL('clicked()'), self.down_page)
def home_page(self):
self.ui.stackedWidget.setCurrentIndex(0)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.output)
def down_page(self):
self.ui.stackedWidget.setCurrentIndex(1)
def output(self):
print(self.ui.lineEdit.text())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = project()
myapp.show()
sys.exit(app.exec_())
Any sugestions?
Thanks!!
i want to display form after form that is display after mainwindow.
This is my code. I success display form after mainwindow.
but MainWindow->Form->Form not work....
please help my brain
my brain is very hot...
main.py
class main(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
app=QtGui.QApplication(sys.argv)
myapp=main()
myapp.show()
app.exec_()
mainUI.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created: Tue Mar 17 17:24:48 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import thread
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)
def start(msg):
while 1:
None
def start2(msg):
while 1:
None
global Form
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(722, 265)
MainWindow.setMinimumSize(QtCore.QSize(722, 265))
MainWindow.setMaximumSize(QtCore.QSize(722, 265))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("image/images.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setEnabled(True)
self.label.setGeometry(QtCore.QRect(-30, -50, 751, 341))
self.label.setMouseTracking(False)
self.label.setAcceptDrops(False)
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("image/main.png")))
self.label.setObjectName(_fromUtf8("label"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 722, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menu = QtGui.QMenu(self.menubar)
self.menu.setObjectName(_fromUtf8("menu"))
MainWindow.setMenuBar(self.menubar)
self.action = QtGui.QAction(MainWindow)
self.action.setObjectName(_fromUtf8("action"))
self.menu.addAction(self.action)
self.menubar.addAction(self.menu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.action, QtCore.SIGNAL(_fromUtf8("triggered()")), self.create_child)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def create_child(self):
#here put the code that creates the new window and shows it.
global Form
Form=QtGui.QWidget()
child = release_Form()
child.setupReleaseUi(Form)
thread.start_new_thread(start,(Form,))
Form.show()
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Smart Update", None))
self.menu.setTitle(_translate("MainWindow", "릴리즈노트", None))
self.action.setText(_translate("MainWindow", "릴리즈노트", None))
class release_Form(object):
def setupReleaseUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(499, 406)
self.New = QtGui.QPlainTextEdit(Form)
self.New.setGeometry(QtCore.QRect(20, 20, 104, 301))
self.New.setObjectName(_fromUtf8("New"))
self.Modify = QtGui.QPlainTextEdit(Form)
self.Modify.setGeometry(QtCore.QRect(150, 20, 104, 301))
self.Modify.setObjectName(_fromUtf8("Modify"))
self.Delete = QtGui.QPlainTextEdit(Form)
self.Delete.setGeometry(QtCore.QRect(280, 20, 104, 301))
self.Delete.setObjectName(_fromUtf8("Delete"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(410, 70, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.New, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.Modify.clear)
QtCore.QObject.connect(self.Modify, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.New.centerCursor)
QtCore.QObject.connect(self.Delete, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.Delete.centerCursor)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.create_alert)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
def create_alert(self):
global Form
Form1=QtGui.QWidget()
alert_Form.show()
child1 = alert_Form()
child1.setupAlertUi(Form1)
thread.start_new_thread(start2,(Form,))
Form1.show()
class alert_Form(object):
def setupAlertUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(431, 109)
Form.setMinimumSize(QtCore.QSize(431, 109))
Form.setMaximumSize(QtCore.QSize(431, 109))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(-10, -10, 451, 121))
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("image/alert.png")))
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Alert", None))
you should connect function.
and you call other Form like this
if you want only once call Form, remove thread.
def create_setting(self):
global Dialog
Dialog=QtGui.QDialog()
thread.start_new_thread(start,(Dialog,))
child = settingUI.Ui_Form()
child.setupUi(Dialog)
Dialog.setWindowFlags(Dialog.windowFlags()|QtCore.Qt.WindowSystemMenuHint|QtCore.Qt.WindowMinMaxButtonsHint)
Dialog.show()
I'm trying to create a UI for an existing python program that I've made.
On the widget, I have two text boxes (QlineEdit) that I want to use to get the information for the program to use, a button and a textbrowser that will hold the output of the program.
I've stumbled upon PyQT4, which I installed using homebrew, and QT Creator. I've created the UI and converted it from a mainwindow.ui to a mainwindow.py and now I'm trying to figure out how to get the text input from the UI text boxes and have the program use it.
I cant figure out how to extract the text from the text boxes(QlineEdit) and store them in variables(strings) that the program can then use.
From my understanding, I should create a new .py file and import mainwindow as well as PyQT4.QtCore and PyQT4.QtGui. I dont know where to go from here so any insight would be helpful.
Also, how can I have the program print out to the textbrowser?
Maybe my approach is flawed and I should rebuild my program around the mainwindow.py file?
I apologize if this is all trivial information but I've never used PyQT or Qt creator.
Here is the code that I got from the the converted .ui to .py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Wed Aug 13 15:25:22 2014
# by: PyQt4 UI code generator 4.11.1
#
# 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(480, 385)
MainWindow.setWindowOpacity(1.0)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.textBrowser = QtGui.QTextBrowser(self.centralWidget)
self.textBrowser.setGeometry(QtCore.QRect(100, 120, 256, 192))
self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
self.widget = QtGui.QWidget(self.centralWidget)
self.widget.setGeometry(QtCore.QRect(150, 10, 156, 97))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.widget)
self.verticalLayout_2.setMargin(0)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.lineEdit = QtGui.QLineEdit(self.widget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.lineEdit_2 = QtGui.QLineEdit(self.widget)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.verticalLayout.addWidget(self.lineEdit_2)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.verticalLayout_2.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 480, 22))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.lineEdit.setText)
QtCore.QObject.connect(self.lineEdit_2, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.lineEdit_2.setText)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.lineEdit.setPlaceholderText(_translate("MainWindow", "Enter Case Number", None))
self.lineEdit_2.setPlaceholderText(_translate("MainWindow", "Number of cases", None))
self.pushButton.setText(_translate("MainWindow", "Submit", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Any information helps. Thanks
The way UI works is that the UI forms the main loop of the program. Here mainwindow.py is your main loop. You can import your classes that you created in your program (lets call it code.py) or just paste that code into you mainwindow.py. Then on button press or some other kind of event, you can call a function that execute the main code of your code.py
To answer your other questions regarding how to use the text and print to the textbrowser try something like this:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Wed Aug 13 15:25:22 2014
# by: PyQt4 UI code generator 4.11.1
#
# 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(480, 385)
MainWindow.setWindowOpacity(1.0)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.textBrowser = QtGui.QTextBrowser(self.centralWidget)
self.textBrowser.setGeometry(QtCore.QRect(100, 120, 256, 192))
self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
self.widget = QtGui.QWidget(self.centralWidget)
self.widget.setGeometry(QtCore.QRect(150, 10, 156, 97))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.widget)
self.verticalLayout_2.setMargin(0)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.lineEdit = QtGui.QLineEdit(self.widget)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.lineEdit_2 = QtGui.QLineEdit(self.widget)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.verticalLayout.addWidget(self.lineEdit_2)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.verticalLayout_2.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 480, 22))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.lineEdit.setText)
QtCore.QObject.connect(self.lineEdit_2, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.lineEdit_2.setText)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.lineEdit.setPlaceholderText(_translate("MainWindow", "Enter Case Number", None))
self.lineEdit_2.setPlaceholderText(_translate("MainWindow", "Number of cases", None))
self.pushButton.setText(_translate("MainWindow", "Submit", None))
# Call a function when the button "Submit" is pressed
self.pushButton.clicked.connect(self.OnSubmit)
def OnSubmit(self):
# On submit do the necessary
# here I take the text from the LineEdits that you have
# (ie your Case Number, and number of Cases)
# and print them to the textbrowser below
text = self.lineEdit.text() + ' ' + self.lineEdit_2.text()
self.textBrowser.append(text)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You can find some basic tutorials here: http://zetcode.com/gui/pyqt4/ that can help you understand the properties of your QtGui objects.