【问题标题】:Python PYQT Tabs outside app window [why]应用程序窗口外的 Python PYQT 选项卡 [为什么]
【发布时间】:2016-09-07 13:16:35
【问题描述】:

在 app.exec_() 调用之前添加的选项卡看起来并充当您遇到的任何其他选项卡,但如果在 app.exec_() 调用之后添加另一个选项卡会使新选项卡与主应用程序窗口“分离”。下图:)

为什么?如何让它在窗口内移动?

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

【问题讨论】:

  • 所有Qt GUI相关代码都必须在程序的主线程中运行。如果您查看控制台,您应该会看到一条消息QObject::setParent: Cannot set parent, new parent is in a different thread。新选项卡已分离,因为无法将 md 设置为它的父选项卡。为什么这个运行没有崩溃我不确定

标签: python python-3.x user-interface pyqt pyqt5


【解决方案1】:

它适用于 QTimer 或信号:

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_()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    相关资源
    最近更新 更多