[英]PyQT6/PySide6: How to make a QWidget always on top of screen?
[英]Pyside6/PyQT6: How can I change the font color on click?
我是 pyside6 學習者。 我正在嘗試創建一個基於 pyside6 的時鍾,我想在單擊時更改其字體顏色。 我怎樣才能讓它發揮作用?
以下代碼是該項目的一部分。
class Clock(QWidget):
def __init__(self, parent=None) -> None:
super().__init__(parent)
self.left = 1100
self.top = 800
self.width = 320
self.height = 60
# Menu
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.mouseRightMenu)
# Font
self.font_color = 'blue'
# UI
self.initUI()
def changeFontColor(self, fcolor) -> None:
self.font_color = fcolor
def mouseRightMenu(self, pos) -> None:
self.menu = QMenu(self)
# font color
fontColorMenu = self.menu.addMenu('font color')
self.actionB1 = QAction('black', self)
self.actionB2 = QAction('red', self)
self.actionB3 = QAction('yellow', self)
self.actionB1.triggered.connect(self.changeFontColor('black'))
def initUI(self) -> None:
# geometry of main window
self.setGeometry(self.left, self.top, self.width, self.height)
# label object
self.label = QLabel()
self.label.setAlignment(Qt.AlignCenter)
self.label.setFont(font)
self.label.setStyleSheet(f'color:{self.font_color};')
if __name__ == '__main__':
App = QApplication(sys.argv)
clock = Clock()
clock.show() # show all the widgets
App.exit(App.exec()) # start the app
目前changeFontColor
function 不起作用,我不知道我在這里錯過了什么。
您缺少兩個基本方面。
首先self.font_color
只是一個變量(或者,准確地說,是一個屬性)。
您在initUi()
中明確使用它以及self.label.setStyleSheet()
,但這並沒有“神奇地”使該變量具有交互性。
你所做的永遠行不通,這里有一個基本的解釋:
color = 'blue'
styleSheet = f'color: {color}'
print(styleSheet)
color = 'green'
print(styleSheet)
如果您考慮以上情況,兩個print 語句將返回相同的值:更改color
不會更改styleSheet
。
這主要是因為字符串是不可變類型(因此, styleSheet
永遠不會受到color
的任何變化的影響),而且還因為color
和styleSheet
之間沒有動態關系。
真正改變小部件外觀的是對setStyleSheet()
的顯式調用,更改用於先前調用的字符串內容是完全無用的。 您必須再次調用setStyleSheet()
才能應用它。
然后,您又犯了另一個錯誤:信號連接使用可調用對象,但您明確調用了changeFontColor()
function。
考慮一下:
def myFunction(arg):
print(f'arg is "{arg}"')
return arg
# this is a call (note the parentheses), which will *execute* the function
>>> myFunction('hello')
# and will show the follwing:
arg is "hello"
# while the following will show a different result:
>>> print(myFunction('hello'))
arg is "hello"
hello
# this is a reference, which will do nothing
>>> myFunction
<function __main__.myFunction>
# notice tht difference with the above:
>>> print(myFunction)
<function myFunction at 0xZZZZZZZZ>
使用self.someObject.someSignal.connect(someFunction())
是錯誤的,(除非someFunction
返回對另一個函數的引用)。
您直接調用您的 function(不返回可調用對象),這實際上會提示錯誤。 您可能沒有注意到(IDE 通常無法顯示所有 output 和完整的回溯,並且大多數 Qt 錯誤都不是致命的),但是如果您在終端或提示符下運行程序,您可能會看到一個錯誤,指出TypeError
發生,因為您正在嘗試連接到NoneType
。
您需要做的是:
def changeFontColor(self, fcolor) -> None:
# explicitly call setStyleSheet() with the color name
self.label.setStyleSheet(f'color: {fcolor};')
def mouseRightMenu(self, pos) -> None:
# ...
self.actionB1.triggered.connect(lambda:
self.changeFontColor('black'))
lambda
創建一個 function 引用,但不調用它:當triggered
信號發出時,最終將調用 function。
上面類似於下面的:
def changeFontColor(self) -> None:
self.label.setStyleSheet('color: black')
def mouseRightMenu(self, pos) -> None:
# ...
self.actionB1.triggered.connect(self.changeFontColor)
不同之處在於我們直接調用 function,它在內部使用 static 值 ( 'black'
)。
我強烈建議您對 object 引用、可調用對象和 object 類型進行一些研究。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.