我正在制作一个基于PySide6的浮动时钟,它的主要部分如下
如何使这个程序总是在屏幕上,即使是在全屏模式下?
self.setWindowFlags(Qt.WindowStaysOnTopHint)方法似乎不起作用。
import sys
from PySide6.QtCore import QPoint, Qt, QTime, QTimer
from PySide6.QtGui import QAction, QFont, QMouseEvent
from PySide6.QtWidgets import QApplication, QLabel, QMenu, QVBoxLayout, QWidget
class Clock(QWidget):
def __init__(self, parent=None) -> None:
super().__init__(parent)
self.left = 1100
self.top = 800
self.width = 320
self.height = 60
# UI
self.initUI()
def initUI(self) -> None:
# geometry of main window
self.setGeometry(self.left, self.top, self.width, self.height)
# hide frame
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# font
font = QFont()
font.setFamily("Arial")
font.setPointSize(50)
# label object
self.label = QLabel()
self.label.setAlignment(Qt.AlignCenter)
self.label.setFont(font)
# layout
layout = QVBoxLayout(self, spacing=0)
layout.addWidget(self.label) # add label
self.setLayout(layout)
# timer object
timer = QTimer(self)
timer.timeout.connect(self.showTime)
timer.start(1000) # update the timer per second
self.show()
def showTime(self) -> None:
current_time = QTime.currentTime() # get the current time
label_time = current_time.toString('hh:mm') # convert timer to string
self.label.setText(label_time) # show it to the label
if __name__ == '__main__':
App = QApplication(sys.argv)
clock = Clock()
clock.show() # show all the widgets
App.exit(App.exec()) # start the app发布于 2022-11-22 22:55:07
您正在使用错误的方法单独设置标志。您应该使用以下方法:setWindowFlag而不是setWindowFlags (末尾没有"s“)。这将解决你的问题:
self.setWindowFlag(Qt.WindowStaysOnTopHint, True)
self.setWindowFlag(Qt.FramelessWindowHint, True)要使用setWindowFlags (结尾是"s“),您应该将标志与按位OR组合起来。下面是该C++的( https://doc.qt.io/qt-6/qtwidgets-widgets-windowflags-example.html )Qt示例
https://stackoverflow.com/questions/74411672
复制相似问题