Skip to content

Instantly share code, notes, and snippets.

@jca02266
Created February 21, 2021 16:08
Show Gist options
  • Save jca02266/d5067367718afa78cbe116d363d58e58 to your computer and use it in GitHub Desktop.
Save jca02266/d5067367718afa78cbe116d363d58e58 to your computer and use it in GitHub Desktop.
PyQt programming (transparency window)
import sys
import random
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.labels = []
self.button = QPushButton('exit', self)
self.button.clicked.connect(self.exit)
self.horizon = QHBoxLayout()
self.horizon.addWidget(self.button)
self.setLayout(self.horizon)
self.setGeometry(300, 10, 400, 350)
self.setWindowTitle('QCheckBox')
self.setAttribute(Qt.WA_TranslucentBackground)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.start()
# timer
def start(self):
self.timer = QTimer(self)
self.timer.timeout.connect(self.timeout)
self.timer.start(10)
def stop(self):
self.timer.stop()
def timeout(self):
self.move()
self.timer.start(10)
def add_label(self):
label = QLabel(self)
label.setText("テスト")
label.setFont(QFont("Times", 32, QFont.Bold))
label.move(300, random.randint(0, 300))
label.show()
return label
def move(self):
lives = []
for label in self.labels:
label.move(label.x() - 1, label.y())
if label.x() < 0:
label.hide()
label.deleteLater()
lives.append(self.add_label())
else:
lives.append(label)
self.labels = lives
if random.randint(0, 500) < 10:
self.labels.append(self.add_label())
def exit(self):
exit(0)
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment