如何同时使用PyQT5表格和菜单?

时间:2019-02-01 11:39:28

标签: python pyqt5

我正在使用PyQT5学习Python GUI,并试图同时使用Menu和Table。当我同时希望菜单栏上的菜单和输出中的表时如何声明该类?

对于Menu,我将该类声明为class Window(Wid.QMainWindow):,并且该类正在运行。对于Table,我将该类声明为class Window(Wid.QWidget):,并且输出也没有问题。当我同时希望菜单栏上的菜单和输出中的表时如何声明该类?

import sys import PyQt5.QtGui as Gui import PyQt5.QtWidgets as Wid import PyQt5.QtCore as Cor

--------菜单代码-----------

class Window(Wid.QMainWindow):

def __init__(self):
    super(Window, self).__init__();
    self.title = "Test";
    self.iconname = "test.png";
    self.initWindow();

def initWindow(self):    
    self.setWindowTitle (self.title);
    self.setGeometry (100, 100, 500, 300);
    self.setWindowIcon (Gui.QIcon(self.iconname));

    self.qtMenu();
    self.show ();

def qtMenu(self):
    mainMenu = self.menuBar();
    fileMenu = mainMenu.addMenu('File');

    subFileMenuNew = Wid.QAction('New', self);

--------表代码-----------

class Window(Wid.QWidget): def __init__(self): super(Window, self).__init__(); self.title = "Test"; self.iconname = "test.png"; self.initWindow();

def initWindow(self):    
    self.setWindowTitle (self.title);
    self.setGeometry (100, 100, 500, 300);
    self.setWindowIcon (Gui.QIcon(self.iconname));

    self.qtTable();
    self.boxLayout = Wid.QVBoxLayout();
    self.boxLayout.addWidget(self.tableWidget);
    self.setLayout(self.boxLayout);

    self.show ();

def qtTable(self):
    self.tableWidget = Wid.QTableWidget();
    self.tableWidget.setRowCount(2);
    self.tableWidget.setColumnCount(2);
    self.tableWidget.setItem(0, 0, Wid.QTableWidgetItem("First Name"));
    self.tableWidget.setItem(0, 1, Wid.QTableWidgetItem("Last Name"));

当我同时想要菜单栏上的菜单和输出中的表时,如何声明该类?

1 个答案:

答案 0 :(得分:0)

尝试一下:

import sys 
import PyQt5.QtGui     as Gui 
import PyQt5.QtWidgets as Wid 
import PyQt5.QtCore    as Cor


class Window(Wid.QMainWindow):
    def __init__(self):
        super(Window, self).__init__();
        self.title    = "Test"
        self.iconname = "D:/_Qt/img/py-qt.png"
        self.initWindow()
# +++       
        self.tableWidget = Wid.QTableWidget(2, 2);
        self.tableWidget.setItem(0, 0, Wid.QTableWidgetItem("John"))
        self.tableWidget.setItem(0, 1, Wid.QTableWidgetItem("Pythonic"))    
        self.tableWidget.setHorizontalHeaderLabels(["First Name", "Last Name"])   # +
        self.setCentralWidget(self.tableWidget)                                   # +   
# +++

    def initWindow(self):    
        self.setWindowTitle(self.title)
        self.setGeometry(100, 100, 500, 300)
        self.setWindowIcon(Gui.QIcon(self.iconname))
        self.qtMenu()

    def qtMenu(self):
        self.main_menu = self.menuBar().addMenu("&File") 
        self.newItem  = Wid.QAction("New",  self,  triggered = self.newFile)
        self.exitItem = Wid.QAction("Exit", self, triggered = Wid.qApp.quit) 
        self.main_menu.addAction(self.newItem) 
        self.main_menu.addAction(self.exitItem)         

    def newFile(self):
        print("def newFile(self):")        


if __name__ == '__main__':
    app = Wid.QApplication(sys.argv)
    qt_app = Window()
    qt_app.show()
    sys.exit(app.exec_())

enter image description here