Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Hans-Peter Jansen Newsgroups: de.comp.lang.python Subject: =?utf-8?q?=5BPython-de=5D_Re=3A_New_overriden_and_generated_QLineEdit_class_will_not?= Date: Fri, 08 Oct 2021 18:02:03 +0200 Lines: 231 Message-ID: <2024386.nlPfB5tcfk@xrated> References: <22626993-ba87-4fb8-8a7d-b8c022a3e30en@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de +XGKCkaY8lBzWQ+ML3tvNA2pUZZpygfEXAKSpZAmk4pQ== Authentication-Results: mail.python.org; dkim=none reason="no signature"; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Virus-Scanned: amavisd at 'smile.earth' In-Reply-To: <22626993-ba87-4fb8-8a7d-b8c022a3e30en@googlegroups.com> X-Provags-ID: V03:K1:HagXTsMOsqriAQuQaJI2IKYvnC7/QTywOOcC2u8fdO4ompxyi+k qtT1CQwFLxgq9L7Y2WXvmOhxuRe3A9/Ncs6S/0oby3XHcBGnWvIodwnIhv0EgHwvXx/jIry pnSYlE9CuHGuIO4abvlJUKUPtiY69tEy933sMnHA76cxNLw47XuU4mTZnPGD+mtVYe+3KZv PBi+Sg+5NgJyXHCpSZwYg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:k5+2YpFfe5A=:DccxIy726FUqEuhGXi3JFP 2JgZpTQBNlOUe2ZYGkPknpN/mFTH6QA6lLAAUvpbgbbUe1JX7xEJr/1YIITA+NkfrFTecvwmg tnzC2BaDMlrQlfyWlTMVIXUXcIcXQjCYVFHJPfSqgkdUWcJXOXDR94MI6nopgjqBzUusZQovT MM0rEpGMfnh5j99mn5bvm4kDu7I+FukTPG2nkYVDSbClouzAgcmjdXTFr9eV8L1UuUqDKegVA 9BKk0zxMf0m+VEx70PApnwQ0cDcRLL/9y9m/gtS1OrQCVAHFTx7UxCP443MBvrVK6RHJ4Esjk U2+Q6X3o0BaVJ2OAcL4BopIoiP7QdhOgB6drcyHvX50pFFJfPa4FZMwVUrT04kX233OXF1hAs ppqWmhwsaH0qAc7tigGsnJFwYI3rV/wEDrgK4ZRPQsD9x1h+WR3qaFt59Gr69LxUxJHj92Oz0 3H064P03Z7hSgcEdNBaKYTZ9/Rfl5qoM1XZEpd3aPieSpWCacUrRVLsVUCOunlEupzQgNaMLi DKV1rqiwXLf3dAZ/HvpuWgioBPKTP+NwW7ujdBOuVpagOPfvHqahcpturaNwlbuyAHJj7EIKS OkO2dgn8KxbmhDbtm0nyTXKnGyuEIrxCzTC1ZIoMGekHa2BG66EmVoJDrbVpSyoAIL9Em97Py Lw99pzxCCuYMMI4RGoFL4pm4egu2Cr7DwHjgsZOYjllYr0upfcYaIsq6j9UYCbYYHG1l2XxFe zO3NC1udWk5NH7mSgFLUoZvFuEamlqjaraNpP0NpzRLBL/u+UZqN/QPMviG4HsK3sTCqTreik MaVbsGw Message-ID-Hash: LUZEQRFFMTA6NBBKAO7Y6Q2NYB66GCCU X-Message-ID-Hash: LUZEQRFFMTA6NBBKAO7Y6Q2NYB66GCCU X-MailFrom: hpj@urpla.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-python-de.python.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.5 Precedence: list List-Id: Die Deutsche Python Mailingliste Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Xref: csiph.com de.comp.lang.python:5742 Am Freitag, 8. Oktober 2021, 11:14:54 CEST schrieb Mohsen Owzar: > Hi all > I have a question about a clickable QLineEdit. > After searching some Hints, how to use QMouseEvent, I've written a program > which is working almost good but with a small or perhaps big ugliness and > that is: Below is the imgur link to the screenshots of my program after > clicking the buttons in sequence: https://imgur.com/a/rfcP3qB Ich sch=E4tze, wir k=F6nnen an diesem Punkt in deutsch weitermachen. Vielleicht solltest Du erst mal versuchen, dein Problem m=F6glichst kurz zu= =20 formulieren. Ich kann hier nur eine Ablaufbeschreibung finden. > As you will see in the code blow,=20 Ein whitespace damaged code ist ziemlich unbrauchbar, you know.. > I override the QlineEdit widget and define > a new class " CustomLineEdit " to generate a "clicked" signal, when the > user pressed with mouse on this field. I use this "CustomLineEdit" class > and place it on the MainWindow with the desired "layout". I couldn't figu= re > out, why this widget is placed not in the MainWindow, but outside this > window, as you can see in the top picture of the attached screenshots. Okay, hier sagst du, du willst dein widget im mainWindow platzieren. Das macht man =FCblicherweise mit einer parent/child relationship. Da du ab= er in=20 der Ableitung deiner Klasse diese *aller* Argumente beraubt hast, geht dir= =20 damit dieser Mechanismus (parent=3D..) verloren. Vielleicht nimmst Du mal d= ein=20 __init__() ganz raus, dann solltest Du die default Signatur zur=FCckbekomme= n,=20 und kannst parent auch wieder setzen. > When > I take the line 12 (self.show()) from the code, the generated class will > not appear. The problem here is that at the beginning, the MainWindow with > a LineEdit field should appear at the program start. Then, after clicking > on this field, the keypad should come up outside the MainWindow. Hier widersprichst du dir. Aber ich glaube, ich habe dein Problem trotzdem= =20 richtig interpretiert. Schau mal in die PyQt Beispiele, da solltest du alle Arten von subclassing,= =20 event handling, und so finden. Wichtig sind da noch die return codes aus de= n=20 event handlern.. Gutes Gelingen. Cheers, Pete > Could > anyone see, where my mistake is? > Thanks for any help. > Mohsen > Here is the code: > &&&&&&&&&&&&&&&&&&&&&&&&&&& > import sys > from PyQt5.QtWidgets import (QApplication, QLineEdit, QPushButton, > QMainWindow, QVBoxLayout, QHBoxLayout, QGridLayout, QWidget) > from PyQt5.QtCore import pyqtSignal, pyqtSlot >=20 > class CustomLineEdit(QLineEdit): > clicked =3D pyqtSignal() >=20 > def __init__(self): > super().__init__() >=20 > self.show() >=20 > def mousePressEvent(self, QMouseEvent): > self.clicked.emit() >=20 >=20 > class MainWindow(QMainWindow): > def __init__( self, parent=3DNone ): > super().__init__(parent) >=20 > self.title =3D 'Window 1' > self.left =3D 700 > self.top =3D 300 > self.width =3D 200 > self.height =3D 200 > self.initUI() >=20 > def initUI(self): >=20 > self.keypad_window =3D Keypad_Window(self) >=20 > hbox =3D QHBoxLayout() >=20 > self.cle =3D CustomLineEdit() > self.cle.clicked.connect(self.show_keypad_window) > self.cle.setFixedSize(220, 60) > self.cle.setStyleSheet("color: red;" > "background-color: yellow;" > "font-family: Arial;" > "font-weight: Bold;" > "font-size: 30pt") >=20 > hbox.addWidget(self.cle) > self.setLayout(hbox) >=20 > self.setWindowTitle(self.title) > self.setGeometry(self.left, self.top, self.width, self.height) > self.show() >=20 > def show_keypad_window(self): > self.keypad_window.show() > self.hide() >=20 > def close(self): > self.keypad_window.close() > super(MainWindow, self).close() >=20 > @pyqtSlot(str) > def update_label(self, txt): > self.cle.setText(txt) >=20 > class Keypad_Window(QWidget): > def __init__(self, parent=3DNone): > super().__init__() > self.parent =3D parent >=20 > self.setGeometry(1200, 500, 230, 400) >=20 > vbox =3D QVBoxLayout() > self.display =3D QLineEdit() > self.display.setFixedSize(220, 60) > self.display.setReadOnly(True) > self.display.setStyleSheet("color: Blue; " > "background-color: lightgreen;" > "font-family: Arial;" > "font-weight: Bold;" > "font-size: 18pt") > vbox.addWidget(self.display) >=20 > """Create the buttons.""" > self.buttons =3D {} > self.gridlay =3D QGridLayout() >=20 > self.button_name =3D [['7', '8', '9'], > ['4', '5', '6'], > ['1', '2', '3'], > ['C', '0', '>']] >=20 > self.command_name =3D [['7', '8', '9'], > ['4', '5', '6'], > ['1', '2', '3'], > ['delete', '0', 'accept']] >=20 > for i in range(4): > for j in range(3): > text =3D self.button_name[i][j] >=20 > # keep a reference to the buttons > self.buttons[i, j] =3D QPushButton() > self.buttons[i, j].setText(text) > self.buttons[i, j].setObjectName(text) > self.buttons[i, j].setFixedSize(70, 70) > if i =3D=3D 3: > if j =3D=3D 0: > self.buttons[i, j].setToolTip('Each click deletes\na digit to the left') >=20 > if j =3D=3D 2: > self.buttons[i, j].setToolTip('The whole displayed\nvalue will be taken!') >=20 > self.buttons[i, j].clicked.connect(self.call_button_fun(i, j, > self.command_name)) >=20 > # add to the GridLayout > self.gridlay.addWidget(self.buttons[i, j], i, j) > self.buttons[i, j].setStyleSheet("color: blue; " > "background-color: cyan;" > "font-family: Arial;" > "font-weight: Bold;" > "font-size: 20pt") > vbox.addLayout(self.gridlay) > self.setLayout(vbox) >=20 > def call_button_fun(self, i, j, command_name): > def button_fun(): > if command_name[i][j] =3D=3D self.button_name[i][j]: > displayed_text =3D self.display.text() > self.new_text =3D displayed_text + self.button_name[i][j] > self.display.setText(self.new_text) >=20 > if command_name[i][j] =3D=3D 'accept': > print('>-key pressed!') > self.parent.cle.setText(self.new_text) > self.close() >=20 > if command_name[i][j] =3D=3D 'delete': > print('C-key pressed!') > self.display.setText('') >=20 > return button_fun >=20 > if __name__ =3D=3D "__main__": > app =3D QApplication(sys.argv) > mainwindow =3D MainWindow() >=20 > # Exception abfangen, wenn sie nicht behandelt wurde > sys._excepthook =3D sys.excepthook >=20 > def exception_hook(exctype, value, traceback): > print(exctype, value, traceback) > sys._excepthook(exctype, value, traceback) > sys.exit(1) >=20 > sys.excepthook =3D exception_hook >=20 > sys.exit(app.exec_()) > _______________________________________________ > python-de Mailingliste -- python-de@python.org > Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an > python-de-leave@python.org > https://mail.python.org/mailman3/lists/python-de.python.org/ > Mitgliedsadresse: hpj@urpla.net