PyQt5 window operations Daquan

1, interaction with multiple windows - with the slot function using the signal
'' '
If a window and a window interaction, so as not to access the controls of the window B;
should visit groove binding function of the signal, thereby reducing the coupling between the window

Example: If window B a direct access control, once the window B control is changed, the codes a and B are subject to change

if the access signal B is a, B is close to the change, only need to change the code B, a without changing the code of

the core ideas:
1, the first sub-window design is good, and in the child window which should define the overall signal signal, and the definition of the function of the trigger signal
2, in the main window to define good code inside a custom window object ZWindow (), and its main function signal connections
function 3, a main control window must be associated sub-window opening
4, the code in the main window functions are defined def openZIWindow good open child windows (Self)
'' '

* PyQt5.QtCore Import from
from PyQt5.QtGui * Import
from PyQt5.QtWidgets * Import
from DateDailog Import DateDialog
Import SYS
class a MU (the QWidget):
DEF the __init __ (Self):
S UPER (a MU, Self) __ .__ the init ()
self.setWindowTitle ( "interaction with multiple windows - signals and slots")

= QLineEdit self.line ()
self.b, the QPushButton = ( "open button")
self.b.clicked.connect (self.opendialog) opens the child window control buttons is defined
self.layout = QVBoxLayout, ()
self.layout.addWidget (Self .B)
self.layout.addWidget (self.line)
self.setLayout (self.layout)

DEF OpenDialog (Self):
D = DateDialog (Self)
d.datetime.dateTimeChanged.connect (self.showdate1) method # 1 directly introduced module control method
d.signal.connect (self.showdate2) # method 2 may be connected using a signal
d.show ()

DEF showdate1 (Self, DATE):
self.line.setText (Date.toString ())

DEF showdate2 ( Self, DATE):
self.line.setText (DATE)

IF __name__ __ == "__ main__":
app=QApplication(sys.argv)
p=MU()
p.show()
sys.exit(app.exec_())
子窗口类定义代码:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class DateDialog(QDialog):
signal=pyqtSignal(str)

def __init__(self,parent=None):
super(DateDialog,self).__init__(parent)
self.setWindowTitle("QDateDialog")

layout=QVBoxLayout()
self.datetime=QDateTimeEdit()
self.datetime.setCalendarPopup(True)
self.datetime.setDateTime(QDateTime.currentDateTime())

self.datetime1 = QDateTimeEdit()
self.datetime1.setCalendarPopup(True)
self.datetime1.setDateTime(QDateTime.currentDateTime())

layout.addWidget(self.datetime)
layout.addWidget (self.datetime1)

Button = QDialogButtonBox (QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button.accepted.connect (self.accept) # associated method of receiving system
button.rejected.connect (self.reject) # associated rejected Method

self.datetime1.dateTimeChanged.connect (self.emit1)
layout.addWidget (the Button)
self.setLayout (layout)

DEF dateTime (Self):
return self.datetime.dateTime () # get the current date and time

# trigger function
emit1 DEF (Self):
D = self.datetime1.dateTime () toString ().
self.signal.emit (D)

# define a static method
@staticmethod
DEF GetDateTime (parent = None):
dailog = DateDialog (parent)
# display this window
result = dailog.exec () # display state of the window, or does not accept receiving
DATE = dailog.dateTime ()
return (date.date (), date.time (), Result == QDialog.Accepted)

2、多窗口交互-不使用信号与槽
'''
不使用信号与槽函数的方式一
Win1与Win2之间的交互
相互之间直接访问窗口上的控件,属于一种强烈耦合的方式交互

核心思想:
1、先在窗口2中导入窗口1定义好的类;
2、在窗口2的代码中定义窗口1的类
3、直接将窗口2中的控件信号连接窗口2类的方法

'''
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

#DateDailog窗口类先定义好
from DateDailog import DateDialog #导入类的方式相互之间访问

class Mulwindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("多窗口交互:不使用信号与槽函数")

self.line=QLineEdit(self)
self.b1=QPushButton("弹出对话框1")
self.b1.clicked.connect(self.onb1)

self.b2=QPushButton("弹出对话框2")
self.b2.clicked.connect(self.onb2)

g=QGridLayout()
g.addWidget(self.line)
g.addWidget(self.b1)
g.addWidget(self.b2)
self.setLayout(g)

def onb1(self):
dialog=DateDialog(self)
result=dialog.exec() #先要显示第一个窗口
date=dialog.dateTime()
self.line.setText(date.date().toString()) #显示出来日期,转为字符串
dialog.destroy() #销毁窗口

def onb2(self):

date,time,result=DateDialog.getdatetime()
self.line.setText(date.toString())

if result==QDialog.Accepted: #如果点击接受方法
print("点击确定按钮")
else: #如果点击拒绝方法
print("点击取消按钮")

if __name__=="__main__":
app=QApplication(sys.argv)
p=Mulwindow()
p.show()
sys.exit(app.exec_())

3、设置窗口样式
'''
窗口的样式设置,可以使用一定的方法
'''

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Windowpattern(QMainWindow):
def __init__(self):
super(Windowpattern,self).__init__()
self.setWindowTitle("设置窗口样式")
self.resize(500,260)

self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowStaysOnTopHint) #设置窗口样式,另外 Qt.FramelessWindowHint是指无边框窗口
self.setObjectName("mainwindow") #窗口ID名称,后续直接可以进行访问和使用
self.setStyleSheet("#mainwindow{border-image:url(image/python.png);}") #设置窗口显示图片,即背景图片

if __name__=="__main__":
app=QApplication(sys.argv)
p=Windowpattern()
p.show()
sys.exit(app.exec_())

4、设置窗口风格
'''
窗口的绘图与特效,设置窗口风格QApplication.setStyle(...)
设置窗口中控件的风格
'''

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
import sys

print(QStyleFactory.keys())

class Window(QWidget):
def __init__(self):
super(Window,self).__init__()
self.setWindowTitle("设置窗口风格")
h=QHBoxLayout()

self.stylelabel=QLabel("设置窗口风格")
self.styleComboBox=QComboBox()
self.styleComboBox.addItems(QStyleFactory.keys())

#获取当前窗口的显示风格
print(QApplication.style().objectName())

index=self.styleComboBox.findText(QApplication.style().objectName(),QtCore.Qt.MatchFixedString)
self.styleComboBox.setCurrentIndex(index)

#将所选窗口风格与展示函数连接
self.styleComboBox.activated[str].connect(self.handlestylechanged)

h.addWidget(self.stylelabel)
h.addWidget(self.styleComboBox)
self.setLayout(h)

def handlestylechanged(self,style):
QApplication.setStyle(style)

if __name__=="__main__":

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

5、代码实现窗口的最大化与最小化
'''
窗口的最大化与最小化
'''
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class Windowmaxmin(QWidget):
def __init__(self):
super(Windowmaxmin,self).__init__()
self.setWindowTitle("设置窗口最大化与最小化")
self.resize(500,260)

#设置实现窗口的最大化与最小化以及关闭按钮功能
self.setWindowFlags(Qt.WindowMaximizeButtonHint|Qt.WindowMinimizeButtonHint|Qt.WindowCloseButtonHint)

#使得窗口充满整个桌面
self.b1=QPushButton("窗口最大化1")
self.b1.clicked.connect(self.maxwindow)
#利用内置的函数来进行最大化展示窗口
self.b2=QPushButton("窗口最大化2")
self.b2.clicked.connect(self.showMaximized)
#利用内置的函数来进行最小化展示窗口
self.b3=QPushButton("窗口最小化")
self.b3.clicked.connect(self.showMinimized)

layout=QVBoxLayout()
layout.addWidget(self.b1)
layout.addWidget(self.b2)
layout.addWidget(self.b3)
self.setLayout(layout)

def maxwindow(self):
desktop=QApplication.desktop()
#获取桌面可用尺寸
rect=desktop.availableGeometry()
self.setGeometry(rect)

if __name__=="__main__":
app=QApplication(sys.argv)
p=Windowmaxmin()
p.show()
sys.exit(app.exec_())

7、实现窗口白板绘图程序-项目实践
'''
窗口绘图
1、如何绘图
在painEvent中绘图,使用update方法来触发painevent的调用
2、在哪里绘图
在白色背景得到对象中绘制图像
3、如何通过鼠标实现绘图
按住鼠标左键进行绘图,左键抬起则不绘制
鼠标拥有三个事件:
鼠标按下mousePressEvent
鼠标移动mouseMoveEvent
鼠标抬起mouseReleaseEvent

'''
from PyQt5.QtCore import Qt,QPoint
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QPainter,QPixmap
import sys

class Windowpaint(QWidget):
def __init__(self,parent=None):
super(Windowpaint,self).__init__(parent)
self.setWindowTitle("窗口实现绘图功能")
self.pix=QPixmap()
self.lastpoint=QPoint()
self.endpoint=QPoint()
self.initUI()

def initUI(self):
self.resize(1000,1000)

#画布大小为1000*1000,背景为白色
self.pix=QPixmap(1000,1000) #创建图像
self.pix.fill(Qt.white)

def paintEvent(self, event):
pp=QPainter(self.pix)
#根据鼠标指针前后两个位置绘制直线
pp.drawLine(self.lastpoint,self.endpoint)
#让前一个坐标值等于后一个坐标值
#这样可以实现连续得到线
self.lastpoint=self.endpoint
painter=QPainter(self)
painter.drawPixmap(0,0,self.pix)

def mousePressEvent(self, event):
if event.button()==Qt.LeftButton:
self.lastpoint=event.pos()

def mouseMoveEvent(self, event):
if event.buttons() and Qt.LeftButton:
self.endpoint=event.pos()
self.update()

def mouseReleaseEvent(self, event):
if event.button()==Qt.LeftButton:
self.endpoint=event.pos()
#进行重新绘制
self.update()

if __name__=="__main__":
app=QApplication(sys.argv)
p=Windowpaint()
p.show()
sys.exit(app.exec_())







Guess you like

Origin www.cnblogs.com/Yanjy-OnlyOne/p/12318880.html