pythongui日历控件_小渣渣学习笔记 python day47【PyQt5 日历控件 菜单栏 工具栏 状态栏 打印 】...

#日历控件 QCalendarWidget

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class QCalendarWidgetDemo(QWidget):

def __init__(self):

super(QCalendarWidgetDemo,self).__init__()

self.initUI()

def initUI(self):

self.calendar = QCalendarWidget(self) #这里没有加布局控件

# self.calendar.setGridVisible(True) #日历网格

self.calendar.setMinimumDate(QDate(1999,1,1,))

self.calendar.setMaximumDate(QDate(2099,1,1))

self.calendar.move(50,50)

self.setWindowTitle('日历控件')

self.calendar.clicked.connect(self.showDate)

self.resize(400,400)

date = self.calendar.selectedDate()

self.label = QLabel(self) #这里要加 继承关系

self.label.setText(date.toString('yyyy-MM-dd dddd'))

self.label.move(150,380)

def showDate(self):

self.label.setText((self.calendar.selectedDate().toString('yyyy-MM-dd dddd')))

if __name__ == '__main__':

app = QApplication(sys.argv)

main = QCalendarWidgetDemo()

main.show()

sys.exit(app.exec_())

#日历控件二 QDateTimeEdit

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class QDateTimeEditDemo(QWidget):

def __init__(self):

super(QDateTimeEditDemo,self).__init__()

self.initUI()

def initUI(self):

self.resize(100,100)

self.datetimeEdit = QDateTimeEdit()

self.datetimeEdit.setDisplayFormat('yyyy-MM-dd HH:mm:ss')

self.datetimeEdit.dateChanged.connect(self.dateChanged) #日期变化信号绑定槽 这里到底是怎么传进函数里的date参数的?

self.datetimeEdit.timeChanged.connect(self.timeChanged) #时间变化信号绑定槽

self.datetimeEdit.dateTimeChanged.connect(self.dateTimeChanged) #日期或时间变化绑定槽

self.datetimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime())

self.datetimeEdit2.setDisplayFormat('yyyy-MM-dd HH:mm:ss')

self.datetimeEdit3 = QDateTimeEdit(QDate.currentDate())

self.datetimeEdit3.setDisplayFormat('yyyy-MM-dd')

self.datetimeEdit4 = QDateTimeEdit(QTime.currentTime())

self.datetimeEdit4.setDisplayFormat('HH:mm:ss')

self.datetimeEdit5 = QDateTimeEdit()

self.datetimeEdit5.setCalendarPopup(True) #设置下拉框 弹出日历框 直接选择日期

self.datetimeEdit5.setDisplayFormat('yyyy-MM-dd HH:mm:ss')

self.button = QPushButton('获取日期和时间')

self.button.clicked.connect(self.buttonClicked)

layout = QVBoxLayout(self)

layout.addWidget(self.datetimeEdit)

layout.addWidget(self.datetimeEdit2)

layout.addWidget(self.datetimeEdit3)

layout.addWidget(self.datetimeEdit4)

layout.addWidget(self.datetimeEdit5)

layout.addWidget(self.button)

def dateChanged(self,date):

print(date) #PyQt5.QtCore.QDateTime(2001, 1, 1, 0, 0) #绑定信号和槽的时候,参数中self是类对象本身,

print(type(date)) #

def timeChanged(self,time):

print(time) #PyQt5.QtCore.QDate(2001, 1, 1)

def dateTimeChanged(self,dateTime):

print(dateTime)

def buttonClicked(self):

print(self.datetimeEdit.dateTime())

print(self.datetimeEdit.minimumDate())

print(self.datetimeEdit.maximumDateTime())

if __name__ == '__main__':

app = QApplication(sys.argv)

main = QDateTimeEditDemo()

main.show()

sys.exit(app.exec_())

#菜单QMenuBar

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class MenuDemo(QMainWindow):

def __init__(self):

super(MenuDemo,self).__init__()

menu = QMenuBar(self)

file = menu.addMenu('文件')

file.addAction('新建')

save = QAction('保存',self)

save.setShortcut('Ctrl+S')

file.addAction(save)

save.triggered.connect(self.process)

edit = menu.addMenu('编辑')

edit.addAction('修改')

edit.addAction('删除')

edit.addAction('设置')

quit = QAction('退出',self)

edit.addAction(quit)

quit.triggered.connect(self.quit)

def process(self):

print(self.sender().text())

def quit(self):

self.close()

if __name__ == '__main__':

app = QApplication(sys.argv)

main = MenuDemo()

main.show()

sys.exit(app.exec_())

#创建和使用工具栏 工具栏三种显示状态:只显示图标,只显示文本,同时显示文本和图标,工具栏和菜单栏区别?菜单栏比较全,工具栏是提高效率响应快速,最好用图标

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class ToolDemo(QMainWindow):

def __init__(self):

super(ToolDemo,self).__init__()

self.initUI()

def initUI(self):

self.setWindowTitle('工具栏演示')

self.resize(300,200)

tb1 = self.addToolBar('File')

tb1.addAction(QAction(QIcon('./images/new.png'),'new',self))

tb1.addAction(QAction(QIcon('./images/open.png'),'open',self))

tb1.addAction(QAction(QIcon('./images/save.png'),'save',self))

tb2 = self.addToolBar('Edit')

tb2.addAction(QAction(QIcon('./images/setting.png'), 'setting', self))

tb2.addAction(QAction(QIcon('./images/back.png'), 'back', self))

tb2.addAction(QAction(QIcon('./images/forward.png'), 'forward', self))

tb2.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) #图标后面加文字 setToolButtonStyle

tb1.actionTriggered.connect(self.toolbtnpressed)#绑定信号和槽 绑定的信号是 actionTriggered

tb2.actionTriggered.connect(self.toolbtnpressed) # 同时绑定一起,在后台看下结果

def toolbtnpressed(self,btn):

print('按下了',btn.text())

if __name__ == '__main__':

app = QApplication(sys.argv)

main = ToolDemo()

main.show()

sys.exit(app.exec_())

#状态栏 QStatusBar

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class StatusBarDemo(QMainWindow):

def __init__(self):

super(StatusBarDemo,self).__init__()

self.initUI()

def initUI(self):

self.setWindowTitle('菜单栏的演示')

self.resize(300,200)

self.statusBar = QStatusBar() #实例化状态栏

self.setStatusBar(self.statusBar) #放在主窗口

menu = self.menuBar() #实例化一个菜单栏

file = menu.addMenu('File') #菜单栏里添加一个菜单

file.addAction('show') #菜单栏添加一个动作

file.triggered.connect(self.processTriggered)

def processTriggered(self,q):

if q.text()=='show':

self.statusBar.showMessage(q.text()+'被点击了',5000)

if __name__ == '__main__':

app = QApplication(sys.argv)

main = StatusBarDemo()

main.show()

sys.exit(app.exec_())

#显示对话框【没成功】

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

from PyQt5.QtPrintSupport import *

class PrintDemo(QWidget): #这里用QWidget,layout才有效,用QMainWindow无效

def __init__(self):

super(PrintDemo,self).__init__()

self.resize(400,300)

self.setWindowTitle('使用打印机')

self.button = QPushButton(self)

self.button.setText('点击打印内容')

self.button.clicked.connect(self.print)

self.textEdit = QTextEdit(self)

# self.textEdit.geometry()

layout = QVBoxLayout(self)

layout.addWidget(self.button)

layout.addWidget(self.textEdit)

def print(self):

printer = QPrinter()

painter = QPainter()

painter.begin(printer)

screen = self.textEdit.grab()

painter.drawPixmap(screen)

painter.end()

if __name__ == '__main__':

app = QApplication(sys.argv)

main = PrintDemo()

main.show()

sys.exit(app.exec_())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个Python GUIPyQt5中QComboBox控件的使用方法和实例: QComboBox是PyQt5中的一个下拉列表框控件,可以方便地在图形界面中实现下拉列表框功能。以下是QComboBox控件的详细使用方法和实例: 1. 在PyQt5中导入QComboBox控件: ``` python from PyQt5.QtWidgets import QComboBox ``` 2. 创建一个QComboBox控件对象: ``` python combo_box = QComboBox() ``` 3. 添加下拉列表框选项: 可以通过addItem()方法添加下拉列表框选项,也可以在创建QComboBox控件时直接传入选项列表: ``` python combo_box.addItem('选项1') combo_box.addItem('选项2') combo_box.addItem('选项3') # 或者 options = ['选项1', '选项2', '选项3'] combo_box = QComboBox(self) combo_box.addItems(options) ``` 4. 获取当前选中的选项: 可以通过currentIndex()方法获取当前选中的选项的索引值,也可以通过currentText()方法获取当前选中的选项的文本内容: ``` python index = combo_box.currentIndex() text = combo_box.currentText() ``` 5. 为下拉列表框添加事件: 可以为下拉列表框添加事件,例如选项改变时触发事件。可以通过currentIndexChanged()方法来实现: ``` python combo_box.currentIndexChanged.connect(self.on_combo_box_changed) def on_combo_box_changed(self, index): # index为当前选中的选项的索引值 print(index) ``` 下面是一个完整的例子: ``` python from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QVBoxLayout import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('QComboBox控件使用例子') self.setGeometry(300, 300, 300, 200) label = QLabel('请选择一项:', self) combo_box = QComboBox(self) combo_box.addItem('选项1') combo_box.addItem('选项2') combo_box.addItem('选项3') combo_box.currentIndexChanged.connect(self.on_combo_box_changed) layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(combo_box) self.setLayout(layout) self.show() def on_combo_box_changed(self, index): print(index) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 该例子创建了一个窗口,包含一个QLabel控件和一个QComboBox控件,当选项改变时会触发on_combo_box_changed()方法。当选项改变时,会在控制台输出当前选中的选项的索引值。 希望这个例子能够帮助你理解使用PyQt5中的QComboBox控件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值