PyQt5 窗口操作
################################ # PyQt5中文网 - PyQt5全套视频教程 # # https://www.PyQt5.cn/ # # 主讲: 村长 # ################################ from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("窗口") self.setWindowOpacity(0.9) self.resize(600, 500) self.btn_x = 50 self.btn_y = 30 self.func_list() def func_list(self): self.btn() def btn(self): self.close_btn = QPushButton(self) self.close_btn.setText('关闭') self.close_btn.resize(self.btn_x, self.btn_y) self.max_btn = QPushButton(self) self.max_btn.setText('最大化') self.max_btn.resize(self.btn_x, self.btn_y) self.min_btn = QPushButton(self) self.min_btn.setText('最小化') self.min_btn.resize(self.btn_x, self.btn_y) def max_signal(): if self.isMaximized(): self.showNormal() self.max_btn.setText('最大化') else: self.showMaximized() self.max_btn.setText('恢复') self.max_btn.pressed.connect(max_signal) self.close_btn.pressed.connect(self.close) self.min_btn.pressed.connect(self.showMinimized) def resizeEvent(self, QResizeEvent): self.close_btn.move(self.width() - self.btn_x, 2) self.max_btn.move(self.width() - self.btn_x * 2, 2) self.min_btn.move(self.width() - self.btn_x * 3, 2) def mousePressEvent(self, QMouseEvent): if QMouseEvent.button() == Qt.LeftButton: self.mouse_press = True # 需要在属性中先定义为False self.win_x = self.x() # 窗口位置 self.win_y = self.y() self.m_x = QMouseEvent.globalX() # 全局位置 self.m_y = QMouseEvent.globalY() # 1.创建一个标记,用来判定鼠标只有在按下之后才能移动 # 2.窗口的原始坐标 # 3.鼠标按下的坐标 def mouseMoveEvent(self, QMouseEvent): if self.mouse_press: move_x = QMouseEvent.globalX() move_y = QMouseEvent.globalY() xx = move_x - self.m_x yy = move_y - self.m_y self.move(self.win_x + xx, self.win_y + yy) # if 窗口标记 == True: # 2.根据鼠标按下的点计算移动量 # 3.根据移动量和窗口的原始坐标得到新坐标 # 4.移动串口位置 def mouseReleaseEvent(self, QMouseEvent): self.mouse_press = False # 1.把mousePressEvent中创建的标记重置为False if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())