gpt4 book ai didi

python - PyQt : How to change a label text from a package

转载 作者:太空宇宙 更新时间:2023-11-04 05:09:11 25 4
gpt4 key购买 nike

我的问题是:如何更改包装中的标签(或其他图形元素)?这个想法是为了减轻我的主要计划。谢谢!

前主程序:

#../mainprogram.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt5 import QtWidgets
from ui import Ui_MainWindow
from package import update

class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)

# label from .ui -> .py
self.ui.label_1.setText("need to change this")

def update_label(self):
self.update = update.label_update()

前包装:

#../package/update.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def label_update():
self.ui.label_1.setText("no problem")

最佳答案

您需要做的是将对象的实例传递给函数。考虑:

def label_update():
self.ui.label_1.setText("no problem")

在这个范围内,我们不知道 self 是什么,因为它还没有被定义。但是,如果您通过 self 传递:

#../mainprogram.py
class MainWindow(QtWidgets.QMainWindow):
def update_label(self):
self.update = update.label_update(self)


#../package/update.py
def label_update(obj): #obj is the object self
obj.ui.label_1.setText("no problem")

我们正在更新传递给函数的对象。

关于python - PyQt : How to change a label text from a package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com