应用程序窗口外的Python PYQT选项卡[为什么]

2024-05-17 13:57:56 发布

您现在位置:Python中文网/ 问答频道 /正文

之前添加的选项卡应用程序执行_()被称为look and act,与您遇到的任何其他选项卡一样,但是如果在应用程序执行_()调用使新选项卡从主应用程序窗口“分离”。下图:)

为什么?我怎样才能让它在窗户里移动?你知道吗

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


Tags: fromimport应用程序mdpyqt5选项卡layoutqwidget
1条回答
网友
1楼 · 发布于 2024-05-17 13:57:56

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

相关问题 更多 >