Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:30:35+00:00 2026-05-31T04:30:35+00:00

I’m creating a Qt application using PyQt4 in Python. I have chosen the Dialog

  • 0

I’m creating a Qt application using PyQt4 in Python. I have chosen the Dialog as my top level application widget. I have read and understood the Default and AutoDefault properties of Push Buttons. I want the following behavior: the buttons respond when clicked or when they have focus (via Tab) and a user presses Enter. I also want them to change color when they have the focus. I don’t want them to respond to Enter when they do not have focus. This is because I have a TextEdit box for which I want Enter to interpreted literally as a CR. If I enable AutoDefault on the buttons, one is chosen as a default (or I can designate it) and that button gets activated if I press enter. It is highlighted all the time, except when another button has the focus. If I disable AutoDefault, neither of the buttons are highlighted, even when they have the focus, and they do not respond to enter, but only space, when they have the focus. This is leaving the user blind as to where the focus is.

My question is: do I need to write a custom event handler to enable the desired behavior? A corresponding question is: Am I better off not using a Dialog as my top level.

I am using Qt Designer and pyqtuic. Here is an excerpt of the code for one of the two buttons (the other one has the same operations):

    self.QuitButton = QtGui.QPushButton(Dialog)
    self.QuitButton.setFocusPolicy(QtCore.Qt.StrongFocus)
    self.QuitButton.setAutoDefault(False)

Full code is available, if requested.

Thank you.


Edit 2/18 7:00 AM. Full code follows. The first of two modules is the one created by pyuic from a designer .ui file is “email.py”:

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'email.ui'
#
# Created: Thu Feb 16 20:12:55 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(643, 480)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Dialog.sizePolicy().hasHeightForWidth())
        Dialog.setSizePolicy(sizePolicy)
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(184, 255, 189))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 255, 130))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(37, 170, 48))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(155, 255, 163))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(184, 255, 189))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 255, 130))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(37, 170, 48))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(155, 255, 163))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipText, brush)
        brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(184, 255, 189))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 255, 130))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(37, 170, 48))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipText, brush)
        Dialog.setPalette(palette)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(16)
        Dialog.setFont(font)
        self.body = QtGui.QTextEdit(Dialog)
        self.body.setGeometry(QtCore.QRect(20, 90, 601, 341))
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.body.sizePolicy().hasHeightForWidth())
        self.body.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(18)
        self.body.setFont(font)
        self.body.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.body.setObjectName(_fromUtf8("body"))
        self.bodylabel = QtGui.QLabel(Dialog)
        self.bodylabel.setGeometry(QtCore.QRect(20, 60, 141, 31))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        self.bodylabel.setFont(font)
        self.bodylabel.setObjectName(_fromUtf8("bodylabel"))
        self.subjectlabel = QtGui.QLabel(Dialog)
        self.subjectlabel.setGeometry(QtCore.QRect(20, 16, 71, 31))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        self.subjectlabel.setFont(font)
        self.subjectlabel.setObjectName(_fromUtf8("subjectlabel"))
        self.SendButton = QtGui.QPushButton(Dialog)
        self.SendButton.setGeometry(QtCore.QRect(336, 440, 121, 31))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Light, brush)
        self.SendButton.setPalette(palette)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(14)
        self.SendButton.setFont(font)
        self.SendButton.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.SendButton.setAutoDefault(False)
        self.SendButton.setObjectName(_fromUtf8("SendButton"))
        self.QuitButton = QtGui.QPushButton(Dialog)
        self.QuitButton.setGeometry(QtCore.QRect(470, 440, 121, 31))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(14)
        self.QuitButton.setFont(font)
        self.QuitButton.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.QuitButton.setAutoDefault(False)
        self.QuitButton.setObjectName(_fromUtf8("QuitButton"))
        self.subject = QtGui.QLineEdit(Dialog)
        self.subject.setGeometry(QtCore.QRect(90, 16, 531, 31))
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.subject.sizePolicy().hasHeightForWidth())
        self.subject.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(16)
        self.subject.setFont(font)
        self.subject.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.subject.setObjectName(_fromUtf8("subject"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        Dialog.setTabOrder(self.body, self.SendButton)
        Dialog.setTabOrder(self.SendButton, self.QuitButton)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Send Message to \"afamily\"", None, QtGui.QApplication.UnicodeUTF8))
        self.bodylabel.setText(QtGui.QApplication.translate("Dialog", "Message Body", None, QtGui.QApplication.UnicodeUTF8))
        self.subjectlabel.setText(QtGui.QApplication.translate("Dialog", "Subject", None, QtGui.QApplication.UnicodeUTF8))
        self.SendButton.setText(QtGui.QApplication.translate("Dialog", "Send Email", None, QtGui.QApplication.UnicodeUTF8))
        self.QuitButton.setText(QtGui.QApplication.translate("Dialog", "Cancel-Quit", None, QtGui.QApplication.UnicodeUTF8))

Following is the application code, which is the second of two modules):

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from email import Ui_Dialog

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class DialogWindow(QtGui.QDialog):

    def __init__(self):
        QtGui.QDialog.__init__(self)

        self.ui=Ui_Dialog()  # instantiates the GUI class.
        self.ui.setupUi(self)

        # # callbacks for things (connects)
        QtCore.QObject.connect(self.ui.SendButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.send)
        QtCore.QObject.connect(self.ui.QuitButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.closenow)

        #QtCore.QMetaObject.connectSlotsByName(Dialog)

        # start with focus on Subject box
        QtGui.QWidget.setFocus(self.ui.subject)
        self.ui.body.setTabChangesFocus(True)

    def send(self):
        print("sending")
        self.close()

    def closenow(self):
        print("closing")
        self.close()


def main():
    app = QtGui.QApplication(sys.argv)
    window = DialogWindow()
    window.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:30:36+00:00Added an answer on May 31, 2026 at 4:30 am

    Here is what I finally arrived in solving this problem. I spent quite a bit of time trying to control appearance to my wishes. It is probably a PyQt hack-job with the use of style sheets. At least I understand it! Style sheets are the only way I could find to control some elements of the appearance. First, below find the UI->PY file from the UI I created in Qt Designer.

    from PyQt4 import QtCore, QtGui
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        _fromUtf8 = lambda s: s
    
    class Ui_SendAfamilyEmail(object):
        def setupUi(self, SendAfamilyEmail):
            SendAfamilyEmail.setObjectName(_fromUtf8("SendAfamilyEmail"))
            SendAfamilyEmail.resize(840, 600)
            font = QtGui.QFont()
            font.setFamily(_fromUtf8("Arial"))
            font.setPointSize(16)
            font.setBold(True)
            font.setWeight(75)
            SendAfamilyEmail.setFont(font)
            self.centralwidget = QtGui.QWidget(SendAfamilyEmail)
            self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
            self.verticalLayoutWidget = QtGui.QWidget(self.centralwidget)
            self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 0, 821, 581))
            self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
            self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
            self.verticalLayout.setMargin(0)
            self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
            spacerItem = QtGui.QSpacerItem(40, 7, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
            self.verticalLayout.addItem(spacerItem)
            self.horizontalLayout = QtGui.QHBoxLayout()
            self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
            self.subjectLabel = QtGui.QLabel(self.verticalLayoutWidget)
            self.subjectLabel.setMinimumSize(QtCore.QSize(78, 0))
            self.subjectLabel.setObjectName(_fromUtf8("subjectLabel"))
            self.horizontalLayout.addWidget(self.subjectLabel)
            self.subject = QtGui.QLineEdit(self.verticalLayoutWidget)
            self.subject.setObjectName(_fromUtf8("subject"))
            self.horizontalLayout.addWidget(self.subject)
            self.verticalLayout.addLayout(self.horizontalLayout)
            spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
            self.verticalLayout.addItem(spacerItem1)
            self.bodyLabel = QtGui.QLabel(self.verticalLayoutWidget)
            self.bodyLabel.setObjectName(_fromUtf8("bodyLabel"))
            self.verticalLayout.addWidget(self.bodyLabel)
            self.body = QtGui.QTextEdit(self.verticalLayoutWidget)
            self.body.setTabChangesFocus(True)
            self.body.setUndoRedoEnabled(False)
            self.body.setTabStopWidth(0)
            self.body.setAcceptRichText(False)
            self.body.setObjectName(_fromUtf8("body"))
            self.verticalLayout.addWidget(self.body)
            spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
            self.verticalLayout.addItem(spacerItem2)
            self.horizontalLayout_2 = QtGui.QHBoxLayout()
            self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
            self.send = QtGui.QPushButton(self.verticalLayoutWidget)
            self.send.setObjectName(_fromUtf8("send"))
            self.horizontalLayout_2.addWidget(self.send)
            self.quit = QtGui.QPushButton(self.verticalLayoutWidget)
            self.quit.setObjectName(_fromUtf8("quit"))
            self.horizontalLayout_2.addWidget(self.quit)
            self.verticalLayout.addLayout(self.horizontalLayout_2)
            SendAfamilyEmail.setCentralWidget(self.centralwidget)
            self.subjectLabel.setBuddy(self.subject)
            self.bodyLabel.setBuddy(self.body)
    
            self.retranslateUi(SendAfamilyEmail)
            QtCore.QMetaObject.connectSlotsByName(SendAfamilyEmail)
            SendAfamilyEmail.setTabOrder(self.subject, self.body)
            SendAfamilyEmail.setTabOrder(self.body, self.send)
            SendAfamilyEmail.setTabOrder(self.send, self.quit)
    
        def retranslateUi(self, SendAfamilyEmail):
            SendAfamilyEmail.setWindowTitle(QtGui.QApplication.translate("SendAfamilyEmail", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
            self.subjectLabel.setText(QtGui.QApplication.translate("SendAfamilyEmail", "Subject", None, QtGui.QApplication.UnicodeUTF8))
            self.bodyLabel.setText(QtGui.QApplication.translate("SendAfamilyEmail", "Body", None, QtGui.QApplication.UnicodeUTF8))
            self.send.setText(QtGui.QApplication.translate("SendAfamilyEmail", "Send Mail", None, QtGui.QApplication.UnicodeUTF8))
            self.quit.setText(QtGui.QApplication.translate("SendAfamilyEmail", "Quit, Do Not Send", None, QtGui.QApplication.UnicodeUTF8))
    

    Next you can find the top level code. It has all the hacks in it. Note this application reads contacts from gmail and send the message via gmain.

    import sys
    from PyQt4 import QtCore   # All GUI imports
    from PyQt4 import QtGui
    from PyQt4.Qt import Qt
    from email2 import Ui_SendAfamilyEmail
    
    import smtplib    # Import smtplib for the actual sending function
    import mimetypes  # For guessing MIME type
    import email      # Import the email modules we'll need
    import email.mime.application
    import email.utils as ut
    # these are all needed to get Google's contact
    import atom,gdata.contacts.data,gdata.contacts.client
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        _fromUtf8 = lambda s: s
    
    class MainWindow(QtGui.QMainWindow):
    
        def __init__(self):
        self.myaddress='my.email@gmail.com';self.mypassword='my passord'
    
            QtGui.QMainWindow.__init__(self)
    
            self.ui=Ui_SendAfamilyEmail()  # instantiates the GUI class.
            self.ui.setupUi(self)
    
            # callbacks for things (connects)
        QtCore.QObject.connect(self.ui.send, QtCore.SIGNAL(_fromUtf8("clicked()")), self.send)
            QtCore.QObject.connect(self.ui.quit, QtCore.SIGNAL(_fromUtf8("clicked()")), self.closenow)
    
            # start with focus on Subject box
            QtGui.QWidget.setFocus(self.ui.subject)
    
            # set up label appearance
        style_string = "QLabel { color: rgb(69,0,208); }"
        self.ui.subjectLabel.setStyleSheet(style_string)
        self.ui.bodyLabel.setStyleSheet(style_string)
    
        # set up button appearance
        self.ui.send.setAutoFillBackground(True)
        self.ui.quit.setAutoFillBackground(True)
        style_string = "QPushButton { background-color: rgb(200,200,200); color: rgb(69, 0, 208); font-size: 16pt; border-style: outset; border: 2px solid black; } QPushButton:hover { background-color: orange;} QPushButton:focus { background-color: orange; }"
        self.ui.send.setStyleSheet(style_string)
        self.ui.quit.setStyleSheet(style_string)
    
        # setup text boxes font color and cursor width
        self.ui.subject.setStyleSheet("QLineEdit { color: rgb(69, 0, 208); border: 2px solid orange; }")
        self.ui.body.setStyleSheet("QTextEdit { color: rgb(69, 0, 208); border: 2px solid orange; }")
        self.ui.body.setCursorWidth(5)
        # not available: self.ui.subject.setCursorWidth(5)
    
        self.GetContacts()
    
        def GetContacts(self):
        self.recipients = list()
        client = gdata.contacts.client.ContactsClient(source='email assistant')
        client.ClientLogin(self.myaddress,self.mypassword,client.source)
    
        feed = client.GetGroups()
        for i, entry in enumerate(feed.entry):
            if entry.title.text == 'afamily':
            id = entry.id.text
    
        feed = client.GetContacts()
        while feed:
            for entry in feed.entry:
            printme = False
            for group in entry.group_membership_info:
                if group.href == id:
                printme = True
            if printme:
                for email in entry.email:
                if email.primary:
                    fmted = ut.formataddr(('"'+entry.title.text+'"', \
                               email.address))
                    self.recipients.append(fmted)
            nextfeed = feed.GetNextLink()
            feed = None
            if nextfeed:
            feed = client.GetContacts(uri=nextfeed.href)
    
        feed = None
        client = None
    
    
        def send(self):
            #print('now sending')
            # Create a text/plain message
        msg = email.mime.Multipart.MIMEMultipart()
        msg['Subject'] = self.ui.subject.text().toAscii().data()
        msg['From'] = self.myaddress
        msg['To'] = ','.join(self.recipients)
    
        # The main body is just another attachment
        body1 = self.ui.body.toPlainText().toAscii().data()
        body = email.mime.Text.MIMEText(body1)
        msg.attach(body)
            #print('message is built')
        # send via Gmail server
        s = smtplib.SMTP('smtp.gmail.com',587)
        s.starttls()
        #print('start tls done')
        s.login(self.myaddress, self.mypassword)
        #print('about to send: ' + self.myaddress + '\n' + self.recipients.__str__()+'\n'\
            #      +msg.as_string())
        s.sendmail(self.myaddress,self.recipients,msg.as_string())
        #print('sent email')
        s.quit()
        self.close()
    
    
        def closenow(self):
            self.close()
    
    
        def keyPressEvent(self,event):
        if event.key() == Qt.Key_Enter or event.key() == Qt.Key_Return:
            if self.ui.send.hasFocus():
            self.send()
            if self.ui.quit.hasFocus():
            self.closenow()
    
    
    def main():
        app = QtGui.QApplication(sys.argv)
        window = MainWindow()
        #window.setMinimumSize(640,480)
        window.show()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    The processing of the keystrokes has tab moving between the four widgets. When the buttons have focus, pressing return on them will cause the button to be activated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.