尝试在PyQT动态添加新标签页

时间:2019-01-24 21:57:50

标签: python-3.x pyqt5

我在尝试在PyQT5(python)上动态添加新标签时遇到问题。 更具体地说,该选项卡已添加,但是在您尝试激活该选项卡时,它将弹出一个新的空白窗口!

我试图从相同的窗口类中添加标签,这种方法可以正常工作,但是在添加新标签时,我需要做一些背景工作,因此我创建了Worker类

这是代码:

import os
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QProgressDialog, QTabWidget, QWidget, QTableWidget, QTableWidgetItem
from main_gui import Ui_MainWindow  # importing our generated file

import sys
os.system("C:\Python37\python -m PyQt5.uic.pyuic -x  gui.ui -o main_gui.py")
class Worker(QtCore.QObject):
    loaded = QtCore.pyqtSignal(int, str)
    finished = QtCore.pyqtSignal()

    def __init__(self, window, wid):
        try:
            super().__init__()
            self._window = window
            self._self = wid
        except Exception as e:
            print("Error 433: " + e)
    def run(self):
        try:
            self._self.AddTab("tab 3?")
        except Exception as e:
            print(e)
    def stop(self):
        try:
            self._stop = True
        except Exception as e:
            print("Error 564: " + e)
class mywindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(mywindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.addRow)
        self.ui.tabWidget.setTabText(0, "yo!")
        self.thread = QtCore.QThread()
        # self.actionExit.triggered.connect(self.close)
        # Ui_MainWindow.closeEvent = self.closeEvent
    def addRow(self):
        try:
            self.worker = Worker(self.ui, self)
            self.worker.moveToThread(self.thread)
            self.worker.finished.connect(self.thread.quit)
            self.thread.started.connect(self.worker.run)
            self.thread.finished.connect(self.worker.deleteLater)
            self.AddTab("Tab Inside")
        except Exception as e:
            print(e)
        self.thread.start()
        return
    def AddTab(self, title):
        self.ui.tabWidget.addTab(QTableWidget(), title)
        return
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())

这是UI文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>961</width>
    <height>533</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>911</width>
      <height>471</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="tab">
     <attribute name="title">
      <string>Tab 1</string>
     </attribute>
     <widget class="QTableWidget" name="tableWidget">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>10</y>
        <width>881</width>
        <height>431</height>
       </rect>
      </property>
      <column>
       <property name="text">
        <string>Column 1</string>
       </property>
      </column>
      <column>
       <property name="text">
        <string>Column 2</string>
       </property>
      </column>
      <column>
       <property name="text">
        <string>Col 3</string>
       </property>
      </column>
      <column>
       <property name="text">
        <string>Col 4</string>
       </property>
      </column>
      <column>
       <property name="text">
        <string>Col 5</string>
       </property>
      </column>
     </widget>
    </widget>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>720</x>
      <y>0</y>
      <width>101</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Load Matches</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>961</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

这是截图,可以看到问题所在。 Wrong window nice window

0 个答案:

没有答案