简体   繁体   English

如何在 Pyqt5 .ui 文件中获取子菜单?

[英]How to get submenu In Pyqt5 .ui file?

I want to get the submenu That is QuitProgram <- This is name of submenu but it is in UI file.我想获得 QuitProgram 的子菜单 <- 这是子菜单的名称,但它在 UI 文件中。

How can I get it in a variable set its action to quit program?我怎样才能在变量中设置它的动作以退出程序?

File menu object name is 'actionQuit_FromProgram' <- This is a submenu.文件菜单对象名称是 'actionQuit_FromProgram' <- 这是一个子菜单。

Python File: Python文件:

from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QFileDialog, QMenuBar
from PyQt5.QtGui import *
import sys
import os
import qdarkgraystyle
path = os.path.abspath(os.getcwd())
import qrcode

class Ui(QtWidgets.QMainWindow):
def __init__(self):
    super(Ui, self).__init__()
    uic.loadUi('main.ui', self)
    #self.show()
    self.button = self.findChild(QtWidgets.QPushButton, 'qrgenerator')
    self.button.clicked.connect(self.QrCodeGenerator) # Remember to pass the definition/method, not the return value!
    self.input = self.findChild(QtWidgets.QLineEdit, 'qredit')
    self.button1 = self.findChild(QtWidgets.QPushButton, 'qropen')
    self.button1.clicked.connect(self.OpenQRCode)
    self.menu1 = self.findChild(QMenuBar,'actionQuit_FromProgram')
    print(self.menu1)
    #self.menu1.triggered.connect(qApp.quit)
    self.show()

def QrCodeGenerator(self):
    # Generate QR code
    self.url = qrcode.make(self.qredit.text())
    if self.qredit.text() == '':
        QMessageBox.warning(self, "Error", "Please Type In Something To Generate Qr Code")
    else:
        self.url.save("filename.png","PNG")
def OpenQRCode(self):
    fname = QFileDialog.getOpenFileName(self, 'Open file', 
    path,"Image files (*.jpg *.gif *.png *.svg)")[0]
    self.label_2.setPixmap(QPixmap(fname))

app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(qdarkgraystyle.load_stylesheet())
window = Ui()
app.exec_()

If you're using loadUi (or setupUi if you're using files generated by pyuic), all elements in the object inspector (the tree view that lists all widgets on your UI) become available as instance attributes, according to their object name .如果您使用loadUi (或setupUi如果您使用由 pyuic 生成的文件),则对象检查器(列出 UI 上所有小部件的树视图)中的所有元素都可用作实例属性,根据它们的对象名称

So, if your action is called actionQuit_FromProgram in the inspector, you can directly access it using self.actionQuit_FromProgram .因此,如果您的操作在检查器中称为actionQuit_FromProgram ,您可以使用self.actionQuit_FromProgram直接访问它。

This also means that:这也意味着:

  • all those findChild are absolutely useless: you already have access to self.qrgenerator , self.qredit , etc;所有这些findChild都绝对没用:您已经可以访问self.qrgeneratorself.qredit等;
  • in any case, findChild should be used with the correct class of the object you're looking for: I sincerely doubt that actionQuit_FromProgram is a QMenuBar (so, using findChild(QMenuBar, ...) won't work at all; if it is an action, use findChild(QAction, ...) ; if it's a submenu, use findChild(Qmenu, ...) ;无论如何, findChild应该与您正在寻找的对象的正确类一起使用:我真诚地怀疑actionQuit_FromProgram是 QMenuBar (因此,使用findChild(QMenuBar, ...)根本不起作用;如果它是动作,使用findChild(QAction, ...) ;如果是子菜单,使用findChild(Qmenu, ...)
  • there's usually just one menu bar for each QMainWindow, and it is easily accessible using self.menuBar() ;每个 QMainWindow 通常只有一个菜单栏,并且可以使用self.menuBar()轻松访问;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM