我有这样的东西:
class Main(QWidget):
def __init__(self):
super().__init__()
def init_gui(self):
self.layout = QGridLayout()
self.setLayout(self.layout)
self.button_sett()
self.show()
def button_sett(self):
button = QPushButton()
menu = QMenu()
menu.addAction("Settings", self.switch_page)
menu.addAction("About", self.switch_page)
button.setMenu(menu)
我可以通过某种方式告诉函数textContent
的动作吗?
赞:
def switch_page():
if textContent == "Settings":
.......
有更好的方法吗?我不想编写switch_page_to_settings
和switch_page_to_about
之类的函数,因为我对该按钮有更多操作。
答案 0 :(得分:0)
def button_sett(self):
button = QPushButton()
menu = QMenu()
def action(title):
menu.addAction(title, lambda: self.switch_page(title))
action("Settings")
action("About")
button.setMenu(menu)
def switch_page(self, title):
if title == "Settings":
pass # TODO
lambda等效于:
def action(title):
def switch():
self.switch_page(title)
menu.addAction(title, switch)