pyqt5-从txt文件显示到多个qtextedit

发布于 2025-02-07 05:47:44 字数 2146 浏览 0 评论 0 原文

我是PYQT5中的新手,并且正在从事从TXT文件运行任务的GUI(每行都是一个任务),并且我已经创建了8个QTEXTEDIT,我希望在单击“执行所有任务”按钮后才需要将每条线显示到qtextedit中(例如,第一行将显示在第一行qtextedit中,第二行是第二行qtextedit中的第二行...,但是第九行将显示在第一个qtextedit中...)并通过我的八个QTEXTEDIT继续循环。

假设我有一个带有10行的文件:

hello1 你好2 你好3 你好4 你好5 你好6 你好7 你好8 你好9 Hello10

这是我要达到的结果:

res1

res2

我尝试了一下,但是当我单击“执行所有任务”按钮时,我的表单崩溃了以下消息:

clearzsh:alling python3 main.py

class task(qdialog):

def __init__(self):
    super(Task, self).__init__()
    loadUi("task.ui", self)
    self.exit.clicked.connect(self.taskscreen)
    self.all_b.clicked.connect(self.execute_all)

def taskscreen(self) :
    login = MainScreen()
    widget.addWidget(login)
    widget.setCurrentIndex(widget.currentIndex()+1)
    widget.setFixedHeight(620)
    widget.setFixedWidth(1080)

def execute_all(self) :
    
    all_textedit = []
    t1 = QTextEdit(readOnly=True)
    t2 = QTextEdit(readOnly=True)
    t3 = QTextEdit(readOnly=True)
    t4 = QTextEdit(readOnly=True)
    t5 = QTextEdit(readOnly=True)
    t6 = QTextEdit(readOnly=True)
    t7 = QTextEdit(readOnly=True)
    t8 = QTextEdit(readOnly=True)
    w = [t1,t2,t3,t4,t5,t6,t7,t8]
    
    while not w in all_textedit :
        if isinstance(w, QTextEdit):
            all_textedit.append(w)
            w = w.nextInFocusChain()
        
    with open("taches.txt") as file :
        lines = file.readlines()

    it = itertools.chain(w)
    for line in lines :
        all_textedit = next(it)
        all_textedit.append(line)

app = qapplication(sys.argv(sys.argv) )

widget = qstackedwidget()

=

task.addwidget(task)

(600)

widget.setFixedWidth(1024)

()

task

sys.exit(app.exec_())

widget.setFixedHeight

print("bye")

widget.show

I'm new in pyqt5 and I'm working on a gui that runs tasks from a txt file (each line is a task) and I have 8 QTextEdit's that i already created ,i want after clicking the "execute all tasks" button to display each line into a QTextEdit (for example the first line will be shown in the first QTextEdit, the second line in the second QTextEdit ..., but the ninth line will be displayed in the first QTextEdit , the tenth line in the second QTextEdit ...) and keep looping thru my eight QTextEdit's.

Let's suppose i have a file with 10 lines :

hello1
hello2
hello3
hello4
hello5
hello6
hello7
hello8
hello9
hello10

here is the result i'm trying to reach :

res1

res2

Please check my form

I tried this but my when i click the button " execute all tasks " the form crashes returning this message :

clearzsh: killed python3 main.py

class Task(QDialog):

def __init__(self):
    super(Task, self).__init__()
    loadUi("task.ui", self)
    self.exit.clicked.connect(self.taskscreen)
    self.all_b.clicked.connect(self.execute_all)

def taskscreen(self) :
    login = MainScreen()
    widget.addWidget(login)
    widget.setCurrentIndex(widget.currentIndex()+1)
    widget.setFixedHeight(620)
    widget.setFixedWidth(1080)

def execute_all(self) :
    
    all_textedit = []
    t1 = QTextEdit(readOnly=True)
    t2 = QTextEdit(readOnly=True)
    t3 = QTextEdit(readOnly=True)
    t4 = QTextEdit(readOnly=True)
    t5 = QTextEdit(readOnly=True)
    t6 = QTextEdit(readOnly=True)
    t7 = QTextEdit(readOnly=True)
    t8 = QTextEdit(readOnly=True)
    w = [t1,t2,t3,t4,t5,t6,t7,t8]
    
    while not w in all_textedit :
        if isinstance(w, QTextEdit):
            all_textedit.append(w)
            w = w.nextInFocusChain()
        
    with open("taches.txt") as file :
        lines = file.readlines()

    it = itertools.chain(w)
    for line in lines :
        all_textedit = next(it)
        all_textedit.append(line)

app = QApplication(sys.argv)

widget = QStackedWidget()

task = Task()

widget.addWidget(task)

widget.setFixedHeight(600)

widget.setFixedWidth(1024)

widget.show()

try :

sys.exit(app.exec_())

except :

print("bye")

Thanks for your support !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文