[PyQt] Doubt
Dennis Jensen
djensen at pgcontrols.com
Mon Oct 14 16:09:07 BST 2019
Okay first of all what you posted did not execute due to a few typos and
such so I went and rendered it and added a few adjustments:
import argparse
from sys import exit as sysExit
from pprint import pprint
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout
from PyQt5.QtWidgets import QInputDialog, QLineEdit, QPushButton
# Okay because this Window is so simple there is no reason to
# sub-class it beyond this however if it were a QMainWindow the
# Center Widget would have been the main gui implementation and
# QMainWindow would have been the Controller Class or Main
# Application that coordinates all the elements between the Gui
# and any Data Sources it might have
class TutorialWindow(QWidget):
# The Init had a typo
def __init__(self):
QWidget.__init__(self)
# Okay its a simple thing but every window ought to
# have its own Title
self.setWindowTitle('Tutorial Window')
self.setGeometry(300, 300, 290, 140)
# Using the coordinate system is not PyQt as it was
# designed to use the Layout system as follows
self.btnOpen = QPushButton('Open')
self.btnOpen.clicked.connect(self.showDialog)
self.lneTextName = QLineEdit()
self.lneTextName.setPlaceholderText("Enter your name:")
# This is an invisible horizontal box to put these 2
# widgets on the same line and because we want the
# Textbox to expand with the window we do not tack a
# Stretch object to it
HBox = QHBoxLayout()
HBox.addWidget(self.btnOpen)
HBox.addWidget(self.lneTextName)
# This is a vertical which is only needed because IOError
# wanted to place the previous objects at the top of the
# layout so I add the above horizontal box to the vertical
# box and follow it with a Stretch object
VBox = QVBoxLayout()
VBox.addLayout(HBox)
VBox.addStretch(1)
# We could do this cryptically by passing objects into
# other objects but I find that doing it explicitly helps
# to make things more clean and clear
self.setLayout(VBox)
# This was improperly indented (to far) it is part of the class
# but should not be contained within the Init function
def showDialog(self):
text, result = QInputDialog.getText(self, 'Input Dialog', 'Enter your
name:')
if result == True:
self.lneTextName.setText(str(text))
# Just an Argparse example snippet
def GetCmdLineArgs():
Parsr = argparse.ArgumentParser()
Parsr.add_argument('echo')
ArgVals = Parsr.parse_args()
return ArgVals
if __name__ == '__main__':
# If you are going to do Command Line Arguments I would
# strongly suggest you look into Argparse as it is the
# recommended command-line standard Python parsing module
CmdLneArgs = GetCmdLineArgs()
pprint('Command Line Arguements = ' + CmdLneArgs.echo)
# Using Argparse means QApplication will never import any
# command line objects as such we supply an empty list instead
# Further what QApplication returns is your Main Application
# Event Thread as such I name it accordingly to reduce any
# potential confusion about its true purpose
MainEvntThred = QApplication([])
# While this is often the Main Gui it really is the PyQt
# Main Application especially if you get into any more
# complicated Gui/Application renderings and thus again
# I name it accordingly to reduce confusion about its
# true purpose
MainApp = TutorialWindow()
MainApp.show()
sysExit(MainEvntThred.exec_())
On 10/12/2019 10:02 AM, tanya tanyaradzwa wrote:
> Hello everyone,
>
> Referring to the assignment question l asked for your assistance,
> please find my code herein, and attached is the output on my side.
> Thank you. Tanya
>
> import sys
> from pprint import pprint
> from PyQt5.QtWidgets import QWidget, QPushButton, QInputDialog,
> QLineEdit, QApplication
>
> class TutorialWindow(QWidget):
> def __init(self):
> super().__init__()
> self.btn = QPushButton('Open', self)
> self.btn.move(0, 20)
> self.btn.clicked.connect(self.showDialog)
>
> self.text_name = QLineEdit(self)
> self.text_name.move(100, 22)
> self.text_name.setPlaceholderText("Enter your name:")
>
> self.Geometry(300, 300, 290, 140)
>
> def showDialog(self):
> text, result = QInputDialog.getText(self, 'Input Dialog',
> 'Enter your name:')
> if result ==True:
> self.text_name.setText(str(text))
>
> if __name__ == '__main__':
> app = QApplication(sys.argv)
> pprint("input parameters = " + str(sys.argv))
> tutorial_window = TutorialWindow()
> tutorial_window.show()
> sys.exit(app.exec_())
>
>
> On Oct 4, 2019 14:33, "tanya tanyaradzwa"
> <tanyatanyaradzwa460 at gmail.com <mailto:tanyatanyaradzwa460 at gmail.com>>
> wrote:
>
> Hi Florian,
>
> Thank you for your response.
>
> I will send to you my code and the output on my side.
>
> On Oct 4, 2019 09:56, "Florian Bruhin" <me at the-compiler.org
> <mailto:me at the-compiler.org>> wrote:
>
> Hey Tanya,
>
> On Fri, Oct 04, 2019 at 08:50:50AM +0200, tanya tanyaradzwa wrote:
> > I am struggling with a school assignment. I have researched
> all places
> > online, but l am failing to bring it all together. Please
> assist me.
> >
> > Question:
> >
> > Create an application using PyQt. The user is prompted for
> the name of an
> > animal rescue service. This must be displayed in the UI in
> capital letters.
> > The user is then required to enter a character (letter).
> This must also be
> > displayed on the UI in capital letters. The application
> > must read the name of the animal rescue service as well as
> the character
> > and then count the number of occurrences of the character in
> the animal
> > rescue service name. The count must be displayed.
> >
> > Also required:
> >
> > Error message for incorrect input from user (e.g. no name of
> animal rescue
> > service).
> > Error message of character not found or the character is blank!
> >
> > May you please assist. Thank you.
>
> If you have some specific issues, people here would likely be
> happy to help.
> What do you have so far and where do you run into issues?
>
> This pretty much boils down to "please do my homework for me",
> which at least
> I'm not going to do.
>
> Florian
>
> --
> https://www.qutebrowser.org | me at the-compiler.org
> <mailto:me at the-compiler.org> (Mail/XMPP)
> GPG: 916E B0C8 FD55 A072 |
> https://the-compiler.org/pubkey.asc
> <https://the-compiler.org/pubkey.asc>
> I love long mails! | https://email.is-not-s.ms/
>
>
> _______________________________________________
> PyQt mailing list PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20191014/d7e5d997/attachment.html>
More information about the PyQt
mailing list