2016-09-07 3 views
0

Registerkarten, die vor app.exec_() hinzugefügt wurden, sehen wie alle anderen Registerkarten aus und funktionieren auch dann, wenn Sie nach dem Aufruf von app.exec_() eine neue Registerkarte hinzufügen, um sie zu trennen 'aus dem Haupt-App-Fenster. Bild unten :)Python PYQT Tabs außerhalb des Anwendungsfensters [why]

Warum? Wie kann ich es innerhalb des Fensters bewegen?

import threading 
import time 
import sys 

from PyQt5.QtWidgets import QApplication 
from PyQt5.QtWidgets import QFormLayout 
from PyQt5.QtWidgets import QLineEdit 
from PyQt5.QtWidgets import QTabWidget 
from PyQt5.QtWidgets import QTextEdit 
from PyQt5.QtWidgets import QWidget 

class ATry(threading.Thread): 
    def __init__(self): 
     super().__init__() 

    def run(self): 
     time.sleep(1) 
     anotherTextEdit = QTextEdit() 
     anotherLineEdit = QLineEdit() 
     anotherLayout = QFormLayout() 
     anotherLayout.addRow(anotherTextEdit) 
     anotherLayout.addRow(anotherLineEdit) 
     anotherTab = QWidget() 
     anotherTab.setLayout(anotherLayout) 
     md.addTab(anotherTab, "Outside") 

app = QApplication(sys.argv) 
md = QTabWidget() 

aTextEdit = QTextEdit() 
aLineEdit = QLineEdit() 
layout = QFormLayout() 
layout.addRow(aTextEdit) 
layout.addRow(aLineEdit) 
thisTab = QWidget() 
thisTab.setLayout(layout) 
md.addTab(thisTab, "Inside") 

a = ATry() 
a.start() 
md.show() 

app.exec_() 

Screen describing the problem

+0

Alle Qt GUI bezogenen Code muss in dem Haupt-Thread des Programms ausgeführt werden. Wenn Sie auf die Konsole schauen, sollten Sie die folgende Meldung sehen: QObject :: setParent: Übergeordnetes Element nicht festlegen, neues übergeordnetes Element befindet sich in einem anderen Thread. Der neue Tab wird entfernt, da 'md' nicht als übergeordnetes Element festgelegt werden kann. Warum das ohne Absturz läuft, bin ich nicht sicher – user3419537

Antwort

0

Es arbeitet mit QTimer oder Signale:

import sys 
import time 

from PyQt5.QtCore import QObject 
from PyQt5.QtCore import QThread 
from PyQt5.QtCore import pyqtSignal 
from PyQt5.QtWidgets import QApplication 
from PyQt5.QtWidgets import QFormLayout 
from PyQt5.QtWidgets import QLineEdit 
from PyQt5.QtWidgets import QTabWidget 
from PyQt5.QtWidgets import QTextEdit 
from PyQt5.QtWidgets import QWidget 


class ATry(QThread): 
    def __init__(self, pointer): 
     super().__init__() 
     self.pointer = pointer 

    def run(self): 
     time.sleep(2) 
     self.pointer.emit() 


def addTheTab(): 
    anotherTextEdit = QTextEdit() 
    anotherLineEdit = QLineEdit() 
    anotherLayout = QFormLayout() 
    anotherLayout.addRow(anotherLineEdit) 
    anotherLayout.addRow(anotherTextEdit) 
    anotherTab = QWidget() 
    anotherTab.setLayout(anotherLayout) 
    md.addTab(anotherTab, "Whatever") 


class MyQObject(QObject): 
    trigger = pyqtSignal() 

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

    def connect_and_get_trigger(self): 
     self.trigger.connect(addTheTab) 
     return self.trigger 

    def getGFX(self): 
     app = QApplication(sys.argv) 
     md = QTabWidget() 
     md.show() 
     return app, md 


obj = MyQObject() 
app, md = obj.getGFX() 
addTheTab() 
a = ATry(obj.connect_and_get_trigger()) 
a.start() 

# timer = QTimer() 
# timer.timeout.connect(proba) 
# timer.start(3000) 

app.exec_() 
Verwandte Themen