[Tutor] What is the correct way of signalling and slots in PyQt5?

2019-04-03 Thread Ali M
The application crashes without errors every time any signal is called on
the widgets. how should i signal to my functions?
for example these signals make the app crash:
self.lineEdit.textChanged.connect(self.edit_input)
or
self.listWidget.clicked.connect(self.listClicked)

here is the full code:
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3 as sqlite
import sys

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
super().__init__()
self.initUi()

def initUi(self):

self.centralwidget = QtWidgets.QWidget(MainWindow)

self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.textChanged.connect(self.edit_input)

self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.clicked.connect(self.listClicked)

self.textEdit = QtWidgets.QTextEdit(self.centralwidget)

MainWindow.setCentralWidget(self.centralwidget)

self.db =
sqlite.connect(r'C:\Users\deadmarshal\PycharmProjects\Tim\test.db')
self.cur = self.db.cursor()
self.result = self.cur.execute("SELECT Esperanto FROM Words ORDER
BY Esperanto")
self.items = self.result.fetchall()

for i in self.items:
self.listWidget.insertItems(0, i)

def listClicked(self):
index = self.listWidget.currentRow()
results = self.cur.execute("SELECT English FROM Words WHERE
Esperanto = ?", (index,))
for row in results:
self.textEdit.clear()
self.textEdit.insertPlainText(row)

def edit_input(self):
word_to_esp = {'gx': 'ĝ', 'cx': 'ĉ', 'hx': 'ĥ', 'jx': 'ĵ', 'ux':
'ŭ', 'sx': 'ŝ'}
user_input = self.lineEdit.text()
user_input = user_input.lower()
for i in word_to_esp:
if user_input.__contains__(i):
a = user_input.replace(i, word_to_esp[i])
return self.lineEdit.set(a)


if __name__ == "__main__":

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the correct way of signalling and slots in PyQt5?

2019-04-03 Thread Alan Gauld via Tutor
On 03/04/2019 09:57, Ali M wrote:
> The application crashes without errors every time any signal is called

It's very unusual to get no errors at all. How are you running it?
Are you using an OS terminal? It may be that you are simply
not seeing the errors...

However...

This list is for the core Python language and standard
library and PyQt is not part of that so you may get a
better response on the PyQt forums.

> here is the full code:

I tried running this but (after commenting out the database stuff)
only got the last QTtextEdit widget on screen with no other widgets
visible. Other than that it ran OK insofar as it didn't crash in any way.

A small point:

> for i in word_to_esp:
> if user_input.__contains__(i):

It's considered bad practice to call a dunder method directly.
This should be written as

   if i in user_input:

Which is easier to read.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor