PyQt6 - 通过左键单击 SystemTrayIcon 打开 QWidget
PyQt6 - Open QWidget by left clicking the SystemTrayIcon
我是 PyQt6 的新手 python。我创建了一个 class SystemTrayIcon
,我试图打开一个在 SystemTrayIcon
-class 之外创建的 QWidget win
,以便在 SystemTrayIcon 得到时显示左键单击。
我收到这个错误:"name 'win' is not defined"
我该如何解决这个问题,在 class SystemTrayIcon
之外创建的 win
-QWidget 将在 左键单击 SystemTrayIcon 时打开 ?
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import sys
import ui_main
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, icon, win, parent=None):
QSystemTrayIcon.__init__(self, icon, parent)
self.setToolTip("Test SystemTray")
menu = QMenu(parent)
exit_ = menu.addAction("Exit")
exit_.triggered.connect(lambda: sys.exit())
self.setContextMenu(menu)
self.activated.connect(self.trayiconclicked)
def trayiconclicked(self, reason):
if reason == self.ActivationReason.Trigger:
print("SysTrayIcon left clicked")
win.show() ###### -> ERROR: (<class 'NameError'>, NameError("name 'win' is not defined"), <traceback object at 0x000001B04F5C9580>)
def run():
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
win = QWidget()
tray_icon = SystemTrayIcon(QIcon("images/logo.png"), win)
tray_icon.setVisible(True)
tray_icon.show()
ui = ui_main.Ui_Form()
ui.setupUi(win, tray_icon.geometry().right(), tray_icon.geometry().bottom(),
tray_icon.geometry().width(), tray_icon.geometry().height())
ui.btn_close.clicked.connect(lambda: win.close())
sys.exit(app.exec())
if __name__ == '__main__':
run()
它现在应该可以工作了,你需要在调用它之前先win
它的属性
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, icon, win, parent=None):
QSystemTrayIcon.__init__(self, icon, parent)
self.setToolTip("Test SystemTray")
self.win = win # <----- name it whatever you want ie self.abc will also work
menu = QMenu(parent)
exit_ = menu.addAction("Exit")
exit_.triggered.connect(lambda: sys.exit())
self.setContextMenu(menu)
self.activated.connect(self.trayiconclicked)
def trayiconclicked(self, reason):
if reason == self.ActivationReason.Trigger:
print("SysTrayIcon left clicked")
self.win.show() # <--- if you named you attribute self.abc call self.abc here instead of self.win
注意:
要使变量成为 class 的属性,您需要用 self 定义它,例如,在这里我想将年龄作为属性
class Person:
def __init__(self, name, age):
# assigning attributes
self.age = age
def some_method(self):
# here calling self.name will give you error because you didn't assign it as a attribute
print(self.name)
def some_method_1(self):
# here this will not give error as you have assigned it earlier as a attribute
print(self.age)
p = Person("Bob", 16)
p.some_method_1()
p.some_method()
我是 PyQt6 的新手 python。我创建了一个 class SystemTrayIcon
,我试图打开一个在 SystemTrayIcon
-class 之外创建的 QWidget win
,以便在 SystemTrayIcon 得到时显示左键单击。
我收到这个错误:"name 'win' is not defined"
我该如何解决这个问题,在 class SystemTrayIcon
之外创建的 win
-QWidget 将在 左键单击 SystemTrayIcon 时打开 ?
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import sys
import ui_main
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, icon, win, parent=None):
QSystemTrayIcon.__init__(self, icon, parent)
self.setToolTip("Test SystemTray")
menu = QMenu(parent)
exit_ = menu.addAction("Exit")
exit_.triggered.connect(lambda: sys.exit())
self.setContextMenu(menu)
self.activated.connect(self.trayiconclicked)
def trayiconclicked(self, reason):
if reason == self.ActivationReason.Trigger:
print("SysTrayIcon left clicked")
win.show() ###### -> ERROR: (<class 'NameError'>, NameError("name 'win' is not defined"), <traceback object at 0x000001B04F5C9580>)
def run():
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
win = QWidget()
tray_icon = SystemTrayIcon(QIcon("images/logo.png"), win)
tray_icon.setVisible(True)
tray_icon.show()
ui = ui_main.Ui_Form()
ui.setupUi(win, tray_icon.geometry().right(), tray_icon.geometry().bottom(),
tray_icon.geometry().width(), tray_icon.geometry().height())
ui.btn_close.clicked.connect(lambda: win.close())
sys.exit(app.exec())
if __name__ == '__main__':
run()
它现在应该可以工作了,你需要在调用它之前先win
它的属性
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, icon, win, parent=None):
QSystemTrayIcon.__init__(self, icon, parent)
self.setToolTip("Test SystemTray")
self.win = win # <----- name it whatever you want ie self.abc will also work
menu = QMenu(parent)
exit_ = menu.addAction("Exit")
exit_.triggered.connect(lambda: sys.exit())
self.setContextMenu(menu)
self.activated.connect(self.trayiconclicked)
def trayiconclicked(self, reason):
if reason == self.ActivationReason.Trigger:
print("SysTrayIcon left clicked")
self.win.show() # <--- if you named you attribute self.abc call self.abc here instead of self.win
注意: 要使变量成为 class 的属性,您需要用 self 定义它,例如,在这里我想将年龄作为属性
class Person:
def __init__(self, name, age):
# assigning attributes
self.age = age
def some_method(self):
# here calling self.name will give you error because you didn't assign it as a attribute
print(self.name)
def some_method_1(self):
# here this will not give error as you have assigned it earlier as a attribute
print(self.age)
p = Person("Bob", 16)
p.some_method_1()
p.some_method()