Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #5741
| From | Lars Liedtke <liedtke@punkt.de> |
|---|---|
| Newsgroups | de.comp.lang.python |
| Subject | [Python-de] Re: New overriden and generated QLineEdit class will not |
| Date | 2021-10-08 12:20 +0200 |
| Organization | punkt.de GmbH |
| Message-ID | <834a692d-5af8-d5f4-9d13-6b1a0c667837@punkt.de> (permalink) |
| References | <22626993-ba87-4fb8-8a7d-b8c022a3e30en@googlegroups.com> |
Dear Mohsen,
You have contacted the German Python mailinglist, so almost all
conversations on here are held in German. If you'd like to communicate
in English, it might be a good idea to contact the general Python
mailinglist, whose main language is English. You can connnect to the
general list under python-list@python.org.
Unfortunately I cannot help you with your Problem, but I am sure there
are people knowing QT in conjunction with Python.
Best Regards
Lars
Am 08.10.21 um 11:14 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
> As you will see in the code blow, 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 figure 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.
> 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.
> 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
>
> class CustomLineEdit(QLineEdit):
> clicked = pyqtSignal()
>
> def __init__(self):
> super().__init__()
>
> self.show()
>
> def mousePressEvent(self, QMouseEvent):
> self.clicked.emit()
>
>
> class MainWindow(QMainWindow):
> def __init__( self, parent=None ):
> super().__init__(parent)
>
> self.title = 'Window 1'
> self.left = 700
> self.top = 300
> self.width = 200
> self.height = 200
> self.initUI()
>
> def initUI(self):
>
> self.keypad_window = Keypad_Window(self)
>
> hbox = QHBoxLayout()
>
> self.cle = 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")
>
> hbox.addWidget(self.cle)
> self.setLayout(hbox)
>
> self.setWindowTitle(self.title)
> self.setGeometry(self.left, self.top, self.width, self.height)
> self.show()
>
> def show_keypad_window(self):
> self.keypad_window.show()
> self.hide()
>
> def close(self):
> self.keypad_window.close()
> super(MainWindow, self).close()
>
> @pyqtSlot(str)
> def update_label(self, txt):
> self.cle.setText(txt)
>
> class Keypad_Window(QWidget):
> def __init__(self, parent=None):
> super().__init__()
> self.parent = parent
>
> self.setGeometry(1200, 500, 230, 400)
>
> vbox = QVBoxLayout()
> self.display = 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)
>
> """Create the buttons."""
> self.buttons = {}
> self.gridlay = QGridLayout()
>
> self.button_name = [['7', '8', '9'],
> ['4', '5', '6'],
> ['1', '2', '3'],
> ['C', '0', '>']]
>
> self.command_name = [['7', '8', '9'],
> ['4', '5', '6'],
> ['1', '2', '3'],
> ['delete', '0', 'accept']]
>
> for i in range(4):
> for j in range(3):
> text = self.button_name[i][j]
>
> # keep a reference to the buttons
> self.buttons[i, j] = QPushButton()
> self.buttons[i, j].setText(text)
> self.buttons[i, j].setObjectName(text)
> self.buttons[i, j].setFixedSize(70, 70)
> if i == 3:
> if j == 0:
> self.buttons[i, j].setToolTip('Each click deletes\na digit to the left')
>
> if j == 2:
> self.buttons[i, j].setToolTip('The whole displayed\nvalue will be taken!')
>
> self.buttons[i, j].clicked.connect(self.call_button_fun(i, j, self.command_name))
>
> # 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)
>
> def call_button_fun(self, i, j, command_name):
> def button_fun():
> if command_name[i][j] == self.button_name[i][j]:
> displayed_text = self.display.text()
> self.new_text = displayed_text + self.button_name[i][j]
> self.display.setText(self.new_text)
>
> if command_name[i][j] == 'accept':
> print('>-key pressed!')
> self.parent.cle.setText(self.new_text)
> self.close()
>
> if command_name[i][j] == 'delete':
> print('C-key pressed!')
> self.display.setText('')
>
> return button_fun
>
> if __name__ == "__main__":
> app = QApplication(sys.argv)
> mainwindow = MainWindow()
>
> # Exception abfangen, wenn sie nicht behandelt wurde
> sys._excepthook = sys.excepthook
>
> def exception_hook(exctype, value, traceback):
> print(exctype, value, traceback)
> sys._excepthook(exctype, value, traceback)
> sys.exit(1)
>
> sys.excepthook = exception_hook
>
> 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: liedtke@punkt.de
--
punkt.de GmbH
Lars Liedtke
.infrastructure
Kaiserallee 13a
76133 Karlsruhe
Tel. +49 721 9109 500
https://infrastructure.punkt.de
info@punkt.de
AG Mannheim 108285
Geschäftsführer: Jürgen Egeling, Daniel Lienert, Fabian Stein
Back to de.comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
New overriden and generated QLineEdit class will not be placed on the MainWindow as expected Mohsen Owzar <mohsen.owzar@gmail.com> - 2021-10-08 02:14 -0700
[Python-de] Re: New overriden and generated QLineEdit class will not Lars Liedtke <liedtke@punkt.de> - 2021-10-08 12:20 +0200
[Python-de] Re: New overriden and generated QLineEdit class will not Hans-Peter Jansen <hpj@urpla.net> - 2021-10-08 18:02 +0200
Re: [Python-de] Re: New overriden and generated QLineEdit class will not Mohsen Owzar <mohsen.owzar@gmail.com> - 2021-10-08 21:10 -0700
[Python-de] Re: New overriden and generated QLineEdit class will Hans-Peter Jansen <hpj@urpla.net> - 2021-10-09 15:10 +0200
Re: [Python-de] Re: New overriden and generated QLineEdit class will Mohsen Owzar <mohsen.owzar@gmail.com> - 2021-10-09 10:54 -0700
Re: [Python-de] Re: New overriden and generated QLineEdit class will Mohsen Owzar <mohsen.owzar@gmail.com> - 2021-10-09 11:07 -0700
[Python-de] Re: New overriden and generated QLineEdit class will Marco Bakera <pintman@bakera.de> - 2021-10-10 09:23 +0200
[Python-de] Re: New overriden and generated QLineEdit class will Hans-Peter Jansen <hpj@urpla.net> - 2021-10-10 18:39 +0200
Re: [Python-de] Re: New overriden and generated QLineEdit class will Mohsen Owzar <mohsen.owzar@gmail.com> - 2021-10-10 13:36 -0700
[Python-de] Re: New overriden and generated QLineEdit class will Hans-Peter Jansen <hpj@urpla.net> - 2021-10-11 12:22 +0200
Re: [Python-de] Re: New overriden and generated QLineEdit class will Mohsen Owzar <mohsen.owzar@gmail.com> - 2021-10-11 04:47 -0700
csiph-web