import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QLabel
from PyQt5.QtCore import QPropertyAnimation, QRect
class Popup(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(300, 200)
self.setStyleSheet("background-color: lightgray; border: 1px solid black;")
layout = QVBoxLayout()
self.label = QLabel("This is a popup!")
layout.addWidget(self.label)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Popup Example")
self.setGeometry(100, 100, 800, 600)
self.popup = Popup(self)
self.popup.setGeometry(250, -200, 300, 200) # Initially hidden above the window
self.show_popup_button = QPushButton("Show Popup", self)
self.show_popup_button.clicked.connect(self.show_popup)
self.show_popup_button.setGeometry(50, 50, 200, 50)
self.hide_popup_button = QPushButton("Hide Popup", self)
self.hide_popup_button.clicked.connect(self.hide_popup)
self.hide_popup_button.setGeometry(50, 120, 200, 50)
def show_popup(self):
self.animation = QPropertyAnimation(self.popup, b"geometry")
self.animation.setDuration(500) # Animation duration in milliseconds
start_rect = QRect(250, -200, 300, 200)
end_rect = QRect(250, 50, 300, 200)
self.animation.setStartValue(start_rect)
self.animation.setEndValue(end_rect)
self.animation.start()
def hide_popup(self):
self.animation = QPropertyAnimation(self.popup, b"geometry")
self.animation.setDuration(500) # Animation duration in milliseconds
start_rect = QRect(250, 50, 300, 200)
end_rect = QRect(250, -200, 300, 200)
self.animation.setStartValue(start_rect)
self.animation.setEndValue(end_rect)
self.animation.start()
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
pyqt5抽屉面板例子
于 2024-09-10 13:36:21 首次发布