pyqt4 how to set text from line edit to label on button click - python-3.x

I am trying to design data input form that will add text to a label when the text is entered into a line edit widget when the ok button is pressed, but the answer is completely eluding me:
#!/usr/bin/python3
#-*- coding: utf-8 -*-
"""
Set label text from line edit with
ok click
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.qle = QtGui.QLineEdit(self)
self.qle.move(100, 0)
sometext = self.qle.text
self.lbl = QtGui.QLabel(self)
self.lbl.move(100, 100)
btn = QtGui.QPushButton("Ok", self)
btn.move(30, 100)
btn.clicked.connect(self.buttonClicked)
self.setGeometry(200, 200, 300, 200)
self.show
def buttonClicked(self, sometext):
sender = self.sender()
self.lbl.setText(sometext)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
How do you get this to work please?

You can use this to set text to a label: QLabel.setText("string text")
Combined with: QPushButton.clicked.connect(lambda: func())
And get the text in the Line Edit: QLineEdit.text()
So complete code should look something like:
#!/usr/bin/python3
#-*- coding: utf-8 -*-
"""
Set label text from line edit with
ok click
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.qle = QtGui.QLineEdit(self)
self.qle.move(100, 0)
sometext = self.qle.text
self.lbl = QtGui.QLabel(self)
self.lbl.move(100, 100)
btn = QtGui.QPushButton("Ok", self)
btn.move(30, 100)
self.setGeometry(200, 200, 300, 200)
self.show
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
def setLabelText():
text = qle.text()
ex.lbl.setText(text)
ex.btn.clicked.connect(lambda: setLabelText())
sys.exit(app.exec_())
I can't actually test this code, but it should work fine.
Tell me if you have any problems, and please give us a traceback.
You might also want to consider moving to PyQt5.6 (Slightly older than the newest, but it has the QtDesigner built in. A quick google search should find you the right executable installer, it's not on their website anymore. If you can't find it I can send it to you.)
Hope it works, and wish you luck!
Edit:
I just thought of a simpler way. Should have just said this.
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.btn.clicked.connect(lambda: ex.lbl.setText(qle.text()))
sys.exit(app.exec_())

A bit of a simplified answer. Did some edits to your code. It works fine, and runs successfully.
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.qle = QtGui.QLineEdit(self)
self.qle.move(10, 10)
self.lbl = QtGui.QLabel(self)
self.lbl.setGeometry(10, 55,200,20)
self.lbl.setText("Type Something and Press Ok!")
btn = QtGui.QPushButton("Ok", self)
btn.move(10, 100)
btn.clicked.connect(self.buttonClicked)
self.setGeometry(200, 200, 300, 200)
self.show
def buttonClicked(self):
sender = self.sender()
self.lbl.setText(self.qle.text())
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Also you QLabel was invisible because it was out of your QMainWindow area.
Hope it Helps!

Related

How can i convert this code to PyQt5?

I have this code i want to convert this code from PyQt4 to PyQt5
Here is the code
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
left = QtGui.QFrame(self)
left.setFrameShape(QtGui.QFrame.StyledPanel)
right = QtGui.QFrame(self)
right.setFrameShape(QtGui.QFrame.StyledPanel)
splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter.addWidget(left)
splitter.addWidget(right)
splitter.setStretchFactor(1, 1)
splitter.setSizes([125, 150])
hbox.addWidget(splitter)
self.setLayout(hbox)
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QSplitter')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
How can i convert this code to pyqt5
Here is image show the result of the code
Try it:
import sys
#from PyQt4 import QtGui, QtCore
from PyQt5 import Qt
class Example(Qt.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = Qt.QHBoxLayout(self)
left = Qt.QFrame(self)
left.setFrameShape(Qt.QFrame.StyledPanel)
left.setStyleSheet('background-color:lightblue; color: white; font-weight: bold;')
labelLeft = Qt.QLabel(" \n left = Qt.QFrame(self)", left)
right = Qt.QFrame(self)
right.setFrameShape(Qt.QFrame.StyledPanel)
labelRight = Qt.QLabel(" \n right = Qt.QFrame(self)", right)
splitter = Qt.QSplitter(Qt.Qt.Horizontal)
splitter.addWidget(left)
splitter.addWidget(right)
splitter.setStretchFactor(1, 1)
splitter.setSizes([125, 150])
hbox.addWidget(splitter)
self.setLayout(hbox)
Qt.QApplication.setStyle(Qt.QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Qt.QSplitter')
self.show()
def main():
app = Qt.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

PyQt5 shortcut in menubar is not working

I'm trying to create a simple UI that has the functionality to close when I use the shortcut Ctrl+Q or when I use the Exit into the File menu, to make it clearer I've created a fairly simple UI that goes with the code:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
class MGen(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
exit_action = QAction('Exit', self)
exit_action.setShortcut('Ctrl+Q')
exit_action.triggered.connect(qApp.quit)
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu('&File')
file_menu.addAction(exit_action)
self.setGeometry(0, 0, 500, 500)
self.setWindowTitle('MGen')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
mgen = MGen()
sys.exit(app.exec_())
Could anyone point what I'm doing wrong?

Show sub window in main window

Can't work out how to embed a window in a main window using classes:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Qt4 tutorial using classes
This example will be built
on over time.
"""
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, MainWindow):
super(Form, self).__init__()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
This is supposed to be the single most basic bit of code using classes. How do I get the second window to show please.
As ekhumoro already pointed out, your widget needs to be a child of your mainWindow. However, I do not think that you need to call show for the widget, since it anyways gets called as soon as its parent (MainWindow) calls show. As mata pointed out correctly, the proper way to add a Widget to a MainWindow instance is to use setCentralWidget. Here is a working example for clarification:
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, parent):
super(Form, self).__init__(parent)
self.lbl = QtGui.QLabel("Test", self)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.setCentralWidget(new_window)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

populating combo box with folders on disk using QFileSystemModel

Hi I have written this basic code trying to populate folders underneath the /Users/ directory, but I don't know what I am missing its not populating.
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class MyWindow(QtGui.QWidget):
"""docstring for MyWindow"""
def __init__(self, parent=None):
super(MyWindow, self).__init__()
self.setup()
def setup(self):
fsm = QtGui.QFileSystemModel()
fsm.setRootPath("/Users/")
layout = QtGui.QVBoxLayout()
combo = QtGui.QComboBox()
combo.setModel(fsm)
layout.addWidget(combo)
self.setLayout(layout)
def main():
app = QtGui.QApplication(sys.argv)
win = MyWindow()
win.show()
win.raise_()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I am getting a / in the comobobox instead of the whole list of folders under /Users/ directory.
I think its better to use QFileSystemModel instead of using os.listdir interms of efficiency and will update the view if somebody updates folder or adds folder in the /Users/ directory !
Remember that QFileSystemModel is a hierarchical model, so you need to let the QComboBox know which QModelIndex represents the children you want to display. You do that with QComboBox.setRootModelIndex()
QFileSystemModel.setRootPath() conveniently returns the QModelIndex of the path you set.
So a small change is all you need (tested on Windows) -
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class MyWindow(QtGui.QWidget):
"""docstring for MyWindow"""
def __init__(self, parent=None):
super(MyWindow, self).__init__()
self.setup()
def setup(self):
fsm = QtGui.QFileSystemModel()
index = fsm.setRootPath("/Users/")
layout = QtGui.QVBoxLayout()
combo = QtGui.QComboBox()
combo.setModel(fsm)
combo.setRootModelIndex(index)
layout.addWidget(combo)
self.setLayout(layout)
def main():
app = QtGui.QApplication(sys.argv)
win = MyWindow()
win.show()
win.raise_()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

pyqt4 event programming (signal and slots )

I am trying to change this event code to standard signal-slot format.
But it does not work.
Can I get help?
this is slide number changing to lcd display number.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
vbox.addWidget(self.sld)
self.setLayout(vbox)
#sld.valueChanged.connect(lcd.display)
QtCore.QObject.connect(self.sld, QtCore.SIGNAL(str1), self, QtCore.SLOT("SHOW()"))
#QtCore.pyqtSlot()
def SHOW(str1):
self.lcd.display()
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal & slot')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You were very close with your first attempt. All you need is:
self.sld.valueChanged.connect(self.lcd.display)
(The other lines up to self.setGeometry can be deleted).

Resources