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.
Related
This question already has answers here:
Make QLabel clickable
(4 answers)
Closed 4 years ago.
I'm creating a side menu using pyqt4.
I turned the QLabel (burgermenu) into a picture of a burger menu, and I'm having trouble making it clickable.
My intension is that when I click the burgermenu image, the inner frame (innerframe) hides totally, and when I click burgermenu again; the innerframe appears with its content.
How can I achieve that?
Here is my code:
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
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(479, 381)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.outerframe = QtGui.QFrame(self.centralwidget)
self.outerframe.setGeometry(QtCore.QRect(190, 80, 151, 211))
self.outerframe.setFrameShape(QtGui.QFrame.StyledPanel)
self.outerframe.setFrameShadow(QtGui.QFrame.Raised)
self.outerframe.setObjectName(_fromUtf8("outerframe"))
self.burgermenu = QtGui.QLabel(self.outerframe)
self.burgermenu.setGeometry(QtCore.QRect(70, 10, 71, 20))
self.burgermenu.setObjectName(_fromUtf8("burgermenu"))
self.innerframe = QtGui.QFrame(self.outerframe)
self.innerframe.setGeometry(QtCore.QRect(10, 60, 131, 141))
self.innerframe.setFrameShape(QtGui.QFrame.StyledPanel)
self.innerframe.setFrameShadow(QtGui.QFrame.Raised)
self.innerframe.setObjectName(_fromUtf8("innerframe"))
self.widget = QtGui.QWidget(self.innerframe)
self.widget.setGeometry(QtCore.QRect(20, 30, 91, 99))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.label_2 = QtGui.QLabel(self.widget)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.verticalLayout.addWidget(self.label_2)
self.dateEdit = QtGui.QDateEdit(self.widget)
self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
self.verticalLayout.addWidget(self.dateEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 479, 22))
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", "MainWindow", None))
self.burgermenu.setText(_translate("MainWindow", "burgermenu", None))
self.burgermenu.setPixmap(QPixmap("/Users/Desktop/menu.png"))
self.label_2.setText(_translate("MainWindow", "Date:", 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_())
Here you can use method name mousePressEvent
self.burgermenu.mousePressEvent = self.dosomething
after that in dosomething method you can call method called hide or show for innerframe.
self.innerframe.hide()
or
self.innerframe.show()
and dont forget to add event as parameter in dosomething
you can overwrite the mousePressEvent of the QLabel like:
def setupClickToggle(self):
def mousePressEvent(*args, **kwargs):
self.innerframe.setVisible(not self.innerframe.isVisible())
self.burgermenu.mousePressEvent = mousePressEvent
and you simply call it once in your init or during startup:
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
setupClickToggle(MainWindow)
MainWindow.show()
i've been trying to create a program in Python that randomly creates dinamic labels on a window, using threads. But something is wrong, couls someone help me please?
I've tried using a thread to call the "animation" function, but it just don't works.
CODE:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'bt.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
import sys
import threading
import time
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(473, 700)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(473, 700))
MainWindow.setMaximumSize(QtCore.QSize(473, 700))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 473, 700))
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("images/bg.png")))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(100, 610, 273, 28))
self.label_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.label_2.setText(_fromUtf8(""))
self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("images/click.png")))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_2.mousePressEvent = self.openGame
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Breaking Thread", None))
def openGame(self, event):
GameWindow.show()
MainWindow.close()
t1.start()
t1.join()
class Ui_GameWindow(object):
def setupUi(self, GameWindow):
GameWindow.setObjectName(_fromUtf8("GameWindow"))
GameWindow.resize(473, 700)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(GameWindow.sizePolicy().hasHeightForWidth())
GameWindow.setSizePolicy(sizePolicy)
GameWindow.setMinimumSize(QtCore.QSize(473, 700))
GameWindow.setMaximumSize(QtCore.QSize(473, 700))
self.centralwidget = QtGui.QWidget(GameWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 473, 700))
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("images/bg2.png")))
self.label.setObjectName(_fromUtf8("label"))
GameWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(GameWindow)
QtCore.QMetaObject.connectSlotsByName(GameWindow)
def retranslateUi(self, GameWindow):
GameWindow.setWindowTitle(_translate("GameWindow", "Breaking Thread", None))
def animation(self):
self.centralwidget = QtGui.QWidget(GameWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(60, 60, 30, 30))
self.label_3.setText(_fromUtf8(""))
self.label_3.setPixmap(QtGui.QPixmap(_fromUtf8("images/bg.png")))
self.label_3.setObjectName(_fromUtf8("label_3"))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
GameWindow = QtGui.QMainWindow()
gi = Ui_GameWindow()
gi.setupUi(GameWindow)
t1 = threading.Thread(target=gi.animation())
MainWindow.show()
sys.exit(app.exec_())
You can't do this - GUI operations must happen in the main thread.
If you want to do something in a secondary thread, use signals and slots to communicate with the main thread and do the GUI updates there.
I'm having a problem where when I save the text from QTextEdit as a txt, or rtf, it doesnt save things like underline and font size. Here's the code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
import gi
import signal
gi.require_version('Gtk', '3.0')
import sys
import dbus
import pygtk
import gi
import signal
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
gi.require_version('Notify', '0.7')
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(679, 600)
self.underlined = False
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.saveButton = QtGui.QPushButton(self.centralwidget)
self.saveButton.setGeometry(QtCore.QRect(10, 0, 88, 28))
self.saveButton.setObjectName(_fromUtf8("pushButton"))
self.textEdit = QtGui.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(0, 30, 681, 800))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.fontButton = QtGui.QPushButton(self.centralwidget)
self.fontButton.setGeometry(QtCore.QRect(400, 0, 88, 28))
self.fontButton.setObjectName(_fromUtf8("fontButton"))
self.fontSize = QtGui.QLineEdit(self.centralwidget)
self.fontSize.setGeometry(QtCore.QRect(100, 0, 28, 28))
self.fontSize.setObjectName(_fromUtf8("fontEdit"))
self.fontSize.returnPressed.connect(self.fontButton.click)
self.underlineButton = QtGui.QPushButton(self.centralwidget)
self.underlineButton.setGeometry(QtCore.QRect(130, 0, 28, 28))
self.underlineButton.setObjectName(_fromUtf8("underlineButton"))
self.disableUnderlineButton = QtGui.QPushButton(self.centralwidget)
self.disableUnderlineButton.setGeometry(QtCore.QRect(160, 0, 28, 28))
self.disableUnderlineButton.setObjectName(_fromUtf8("disableUnderlineButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 679, 24))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuTest = QtGui.QMenu(self.menubar)
self.menuTest.setObjectName(_fromUtf8("menuTest"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuTest.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def save(self):
with open('log.rtf', 'w') as yourFile:
yourFile.write(str(self.textEdit.toPlainText()))
def saveFont(self):
self.textEdit.setFontPointSize(int(self.fontSize.text()))
def underline(self):
self.textEdit.setFontUnderline(True)
def disableUnderline(self):
self.textEdit.setFontUnderline(False)
def commander(self):
save(self)
self.textEdit.setHtml('<u>hi</u>')
self.saveButton.clicked.connect(lambda: save(self))
self.fontButton.clicked.connect(lambda: saveFont(self))
self.underlineButton.clicked.connect(lambda: underline(self))
self.disableUnderlineButton.clicked.connect(lambda: disableUnderline(self))
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.saveButton.setText(_translate("MainWindow", "Save text", None))
self.fontButton.setText(_translate("MainWindow", "Save Font", None))
self.menuTest.setTitle(_translate("MainWindow", "test", None))
self.underlineButton.setText(_translate("MainWindow", "Uon", None))
self.disableUnderlineButton.setText(_translate("MainWindow", "Uoff", 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_())
I've attempted to fix this with HTML, and other formats but couldn't get it to work.
It's because you're converting the text to plain text with toPlainText, which doesn't contain any formatting information.
yourFile.write(str(self.textEdit.toPlainText()))
If you want to maintain the formatting, you need to use toHtml.
yourFile.write(str(self.textEdit.toHtml()))
Be aware that this isn't the same thing as rtf. It's not even entirely standard HTML and it will likely display a bit differently if you try to look at it in another HTML viewer besides the QTextEdit. In my experience, the HTML generated from the QTextEdit's is pretty ugly, and only really works well if you plan on only displaying it inside QTextEdits.
I have a list of data which is shown in the Qcombobox (using 'addItems')
Now - if the list is changed by loading data from a file with pushbutton - i don't see the new data in the combobx.
The new data is there (I can print it after loading)
What do I miss?
below is the code for the simplified gui
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
import pandas as pd
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):
self.comboData=['None']
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(456, 172)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.comboBox = QtGui.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(120, 60, 69, 22))
self.comboBox.setObjectName(_fromUtf8("comboBox"))
self.comboBox.setEditable(True)
self.comboBox.addItems(self.comboData)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 60, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.load)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 456, 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 retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Load", None))
def load(self):
fileName=QFileDialog.getOpenFileName(MainWindow,'Load File')
data_pd=pd.read_csv(fileName,index_col=0,na_filter=False)
self.comboData=list(data_pd.var1)
print(self.comboData)
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 only load the data to self.comboData but do not load them to self.comboBox.
At the end of
def load(self):
add the following lines:
self.comboBox.clear() # delete all items from comboBox
self.comboBox.addItems(self.comboData) # add the actual content of self.comboData
As the model used by QComboBox emits the dataChanged() signal whenever the data in an item are changed, the combobox is repainted automatically and it is not necessary to update the combobox by
self.comboBox.update()
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.