使用PySide开发GUI

时间:2017-01-09 22:15:18

标签: python python-3.x pyside

我正在尝试在GUI应用程序中获取按钮和菜单栏。当我运行我的代码时,菜单栏会看到GUI,但看不到按钮。这是我的示例代码。并且代码编译没有任何错误。

import sys
from PySide.QtGui import *
from PySide.QtCore import *

class guiwindow(QMainWindow):

    def __init__(self):
        super(guiwindow,self).__init__()

        self.menubar()

    def menubar(self):
        textEdit = QWidget()
        self.setCentralWidget(textEdit)

        exitAction = QAction('Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(self.close)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

        self.setGeometry(400, 100, 1200, 800)
        self.setWindowTitle("Menubar + Buttons")


        button = QPushButton("Test")
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(button)
        self.setLayout(hbox)  
        self.show()


def main():
    app = QApplication(sys.argv)
    ex = guiwindow()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

通常,在GUI编程中,您必须熟悉父子窗口小部件的概念。如果你想让你的按钮在你的窗口内,那么后者应该是前者的孩子。

所以使用:

load() {
  return Observable.create(s => {
    this.http.get('url.xml')
    .map(res => res.text())
    .subscribe(data => {
      var parser = new DOMParser();
      var xmlData = parser.parseFromString(data, "application/xml");

      // First get the information (title, description, ...)
      var info = {};
      (xmlData.querySelectorAll("channel>*:not(item)") || []).forEach(function(e){
        var infoData = {};
        var found = false;
        e.childNodes.forEach(function(e){
          // skip text nodes
          if(e.nodeType == 3) return;
          found = true;
          infoData[e.tagName] = e.textContent;
        });
        if(found) info[e.tagName] = infoData;
        else info[e.tagName] = e.textContent;
      });

      // Then, get the list of items
      var items = [];
      (xmlData.querySelectorAll("channel>item") || []).forEach(function(item){
        var itemData = {};
        item.childNodes.forEach(function(e){
          // skip over text nodes
          if(e.nodeType == 3) return;

          // get attributes if exist (to support the 'enclosure' element)
          var attr = {};
          var found = false;
          for(var i = 0, atr = e.attributes, l = e.attributes.length; i < l; i++){
            found = true;
            attr[atr[i].name] = atr[i].value;
          }
          if(found) itemData[e.tagName] = attr;
          else itemData[e.tagName] = e.textContent;
        });
        items.push(itemData);
      });
      console.log("INFO: ", info);
      console.log("ITEMS: ", items);
    });
  });
}

而不是:

button = QPushButton("Test", parent=self)

希望这有帮助!