Python 图形界面设计:PyQt6 QGridLayout实现简单发邮件和计算小软件

2023-06-0516:15:48编程语言入门到精通Comments1,872 views字数 5948阅读模式

QGridLayoutQHBoxLayoutQVBoxLayout类似,相当于一个风格布局容器,可以生成一个几行几列的布局。比如生成一个5行4列布局,每行加上一按钮,几行代码可以实现。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/44890.html

# file: calculator.py
#!/usr/bin/python

"""
ZetCode PyQt6 tutorial

In this example, we create a skeleton
of a calculator using QGridLayout.

Author: Jan Bodnar
"""

import sys,PyQt6
from PyQt6.QtWidgets import (QWidget, QGridLayout,
        QPushButton, QApplication)

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        grid = QGridLayout()
        self.setLayout(grid)

        names = ['Cls', 'Bck', '', 'Close',
                 '7', '8', '9', '/',
                 '4', '5', '6', '*',
                 '1', '2', '3', '-',
                 '0', '.', '=', '+']

        positions = [(i, j) for i in range(5) for j in range(4)]

        for position, name in zip(positions, names):

            if name == '':
                continue

            button = QPushButton(name)
            grid.addWidget(button, *position)
        #self.resize(900, 600)
        self.move(600, 300)
        self.setWindowTitle('简易计算器')
        self.setWindowIcon(PyQt6.QtGui.QIcon("logo.jpg"))
        self.show()

def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())

if __name__ == '__main__':
    main()
Python 图形界面设计:PyQt6 QGridLayout实现简单发邮件和计算小软件

显示的内容,可以在初始化按钮时直接带上,QPushButton("选择文件"),这里是把names列表内容做一系列按钮的显示内容,位置信息可以是依次填充,也可以给出行列位置进行填充。这里是一个QWidget,如果是QMainWindow,可把这些按钮与其响应绑定,按下的数字,在状态栏上显示,或者单独设置一个LineEdit进行内容显示,然后进行简易的计算。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/44890.html

下面这个例子,是QGridLayout6行2列,分别加上QLableLineEdit,就是简易的发邮件程序。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/44890.html

# file: review.py
#!/usr/bin/python

"""
ZetCode PyQt6 tutorial

In this example, we create a bit
more complicated window layout using
the QGridLayout manager.

Author: Jan Bodnar
Website: zetcode.com
"""

import sys,PyQt6
from PyQt6.QtWidgets import (QWidget, QLabel, QLineEdit,QPushButton,
        QTextEdit, QGridLayout, QApplication)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        title = QLabel('标题')
        author = QLabel('收件人')
        review = QLabel('内容')

        titleEdit = QLineEdit()
        authorEdit = QLineEdit()
        reviewEdit = QTextEdit()
        b = QPushButton("发送")
        grid = QGridLayout()
        grid.setSpacing(12)

        grid.addWidget(title, 1, 0)
        grid.addWidget(titleEdit, 1, 1)

        grid.addWidget(author, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

        grid.addWidget(review, 3, 0)
        grid.addWidget(reviewEdit, 3, 1, 5, 1)
        grid.addWidget(b, 6, 0)
        self.setLayout(grid)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowIcon(PyQt6.QtGui.QIcon("logo.jpg"))
        self.setWindowTitle('简易邮件程序')
        self.show()


def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

以前写过一个发邮件程序,申请一个163或者QQ邮箱的api key,通过上面的界面,把下面的代码与QPushButton绑定,就可以实现发邮件功能。QPushButton与代码绑定,self.b.clicked.connect(self.sent_email),在类内部再加一个发邮件函数就可以搞定了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/44890.html

smtp_server = 'smtp.qq.com'

html_msg = """
<p>尊敬的领导:</p>
<p>这是黑龙江省*****************,请查收!</p>
"""


# 创建一个带附件的实例msg
msg = MIMEMultipart()
msg['From'] = Header('黑龙江省*********')  # 发送者
msg['To'] = Header('办公室、****司、*****司')  # 接收者
subject = '黑龙江省**********专报'
msg['Subject'] = Header(subject, 'utf-8')  # 邮件主题
# 邮件正文内容
msg.attach(MIMEText(html_msg, 'html', 'utf-8'))


att = MIMEBase('application', 'octet-stream')
att.set_payload(open(filename1, 'rb').read())
att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', filename) )
encoders.encode_base64(att)

msg.attach(att)


try:
    smtpobj = smtplib.SMTP_SSL(smtp_server)
    smtpobj.connect(smtp_server, 465)    # 建立连接--qq邮箱服务和端口号
    smtpobj.login(from_addr, password)   # 登录--发送者账号和口令
    smtpobj.sendmail(from_addr, to_addr, msg.as_string())
    

    print("邮件发送成功")
    time.sleep(30)
except smtplib.SMTPException:
    print("无法发送邮件")
finally:
    # 关闭服务器
    smtpobj.quit()

python代码后台运行效率比较高,可以神不知鬼不觉就把事给办了,总感觉有点low,加上图形界面,似乎好那么一点儿。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/44890.html

显卡之父黄老板在某大演讲,有这么几条:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/44890.html

  1. We are at the beginning of a major technological era: "Like PC, internet, mobile and cloud, but AI is far more fundamental"
  2. 我们正处于一个重大技术时代的开端:“就像 PC、互联网、移动和云一样,但人工智能更为根本”
  1. AI will create new jobs that didn't exist before: "Like prompt engineering, AI Factory ops, and AI safety engineers."
    2.AI 将创造以前不存在的新工作:“比如快速工程、人工智能工厂运营和人工智能安全工程师。”
  1. AI will change every job: "Supercharging the performance of programmers, designers, artists, marketers, and manufacturing planners"
    3.AI 将改变每一份工作:“提高程序员、设计师、艺术家、营销人员和制造规划人员的绩效”
  1. You must learn to take advantage of AI: "While some worry AI may take their jobs, someone who is expert with AI will"
  2. 你必须学会利用人工智能:“虽然有些人担心人工智能可能会抢走他们的工作,但人工智能专家会”
  1. Overall, AI software has opened multi-trillion dollar opportunities: "The world was simpler when I graduated college"
  2. 总体而言,人工智能软件已经开辟了数万亿美元的机会:“大学毕业时,世界更简单”
  1. Nvidia's purpose: "To help the Einstein and DaVinci's of our time do their life's work"
  2. 英伟达的宗旨:“帮助我们这个时代的爱因斯坦和达芬奇完成他们一生的工作”
  1. Nvidia almost died in its first years: Jensen had to call the CEO of Sega and say "that they had to find another partner (because our tech failed) but I needed Sega to pay us in whole or Nvidia would be out of business"
  2. 英伟达在最初的几年几乎死了:詹森不得不打电话给世嘉的首席执行官说:“他们必须找到另一个合作伙伴(因为我们的技术失败了),但我需要世嘉全额支付我们,否则英伟达就会倒闭。
  1. Humility abut his weakness led to his success: Flying to Taiwan and working with Morris Chang on production (where Nvidia was weak) enabled their position today 25 years later
  2. 谦卑与他的软弱导致了他的成功:飞往台湾并与 Morris Chang 合作进行生产(英伟达在英伟达的实力较弱的地方)使他们在 25 年后获得了今天的地位。
  1. Nvidia had to endure years of $1B market cap: Because they were investing in CUDA that created the base for the applications on top of GPUs like blockchain and AI
  2. 英伟达不得不忍受多年的10亿美元市值:因为他们正在投资CUDA,为区块链和人工智能等 GPU 之上的应用程序奠定了基础。
  1. Alexnet, trained on GPUs of Nvidia, in 2012 started the big bang of AI; Nvidia risked everything by doubling down on it and deep learning.
  2. Alexnet,在英伟达的 GPU 上接受培训,在 2012 年开始了人工智能的大爆炸;英伟达冒着一切风险,加倍投入和深度学习。
  1. Sacrificing the profitable mobile market setup the AI opportunity: Nvidia had to leave a profitable business to focus on AI and robotics.
  2. 牺牲有利可图的移动市场设置人工智能机会:英伟达不得不离开盈利的业务,专注于人工智能和机器人技术。
  1. We are the starting line of AI: "Every industry will be revolutionized...Run after it" And why was he at the night market? "Either you are running for food, or you are... food."
  2. 我们是人工智能的起跑线:“每个行业都将发生革命性的变化...... 追着它跑” 他为什么会出现在夜市?“要么你是在为食物而奔跑,要么你...... 食物。
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/44890.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/44890.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定