栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Windows+Python使用PyQt5----简单样例(三)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Windows+Python使用PyQt5----简单样例(三)

Windows+Python使用PyQt5----简单样例(三)
  • 使用PyQT的浮动tip
  • 使用PyQT的toolbar
  • 使用PyQT的浮动tool
  • 使用PyQT的菜单栏
  • 使用PyQT的tab切换组件

使用PyQT的浮动tip
# 鼠标移动到按钮上方,就会现实tip
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        button = QPushButton("Button")
        # 给按钮设置传统的tip
        button.setToolTip("This is a text")
        layout.addWidget(button, 0, 0)

        button = QPushButton("Button")
        # 给按钮设置HTML格式的tip
        button.setToolTip("HTML can be shown too..")
        layout.addWidget(button, 1, 0)

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())


使用PyQT的toolbar
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        # 初始化toolbar
        toolBar = QToolBar()
        layout.addWidget(toolBar)

        # 网toolbar中添加按钮
        toolButton = QToolButton()
        toolButton.setText("Apple")
        toolButton.setCheckable(True)
        toolButton.setAutoExclusive(True)
        toolBar.addWidget(toolButton)
        toolButton = QToolButton()
        toolButton.setText("Orange")
        toolButton.setCheckable(True)
        toolButton.setAutoExclusive(True)
        toolBar.addWidget(toolButton)

        # 添加文本输入框到窗口
        tbox = QPlainTextEdit()
        layout.addWidget(tbox)
        
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())


使用PyQT的浮动tool
from PyQt5.QtWidgets import *
import sys

# addItem()
# count()
# currentIndex()
# insertItem()
# itemToolTip()
# itemText()
# itemIcon()
# isItemEnabled()
# removeItem()
# setItemEnabled()
# setItemIcon()
# setItemText()
# setItemToolTip()

class Window(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        # 添加子项到toolbox
        toolbox = QToolBox()
        layout.addWidget(toolbox, 0, 0)
        label = QLabel()
        toolbox.addItem(label, "Students")
        label = QLabel()
        toolbox.addItem(label, "Teachers")
        label = QLabel()
        toolbox.addItem(label, "Directors")

        # 现实toolbox有多少个子项
        print(toolbox.count())

        # 设置无法编辑的项
        toolbox.setItemEnabled(0, False)

        # 设置鼠标移动到tool时的tip
        toolbox.setItemToolTip(0, "This is a tooltip")

        # 获取状态
        print(toolbox.isItemEnabled(0))
        print(toolbox.isItemEnabled(1))

        # 插入一个新的item
        item = QLabel()
        toolbox.insertItem(1, item, "Python")

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())


使用PyQT的菜单栏
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        # 创建菜单
        menubar = QMenuBar()
        layout.addWidget(menubar, 0, 0)
        actionFile = menubar.addMenu("File")
        # 给File这个按钮添加子项
        actionFile.addAction("New")
        actionFile.addAction("Open")
        actionFile.addAction("Save")
        actionFile.addSeparator()
        actionFile.addAction("Quit")
        menubar.addMenu("Edit")
        menubar.addMenu("View")
        menubar.addMenu("Help")

        # 添加文本输入框
        tbox = QPlainTextEdit()
        layout.addWidget(tbox, 1, 0)

        
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())


使用PyQT的tab切换组件
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)
        label1 = QLabel("Widget in Tab 1.")
        label2 = QLabel("Widget in Tab 2.")
        # 初始化tab组件
        tabwidget = QTabWidget()
        # 把label1放在tab1
        tabwidget.addTab(label1, "Tab 1")
        # 把label2放在tab2
        tabwidget.addTab(label2, "Tab 2")
        layout.addWidget(tabwidget, 0, 0)

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008927.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号