我用python 和pyside6(qt)写了一个图形界面,主要用来操作界面中的表格。
在一个类中调用界面,类中的函数对表格进行操作
class Widget(QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
def table(self):
old = update()
h = 10 # 行,居中为10
l = 0 # 列,起始
while True:
new = update()
if new[ > old:
self.bg.item(h, l).setBackground(QtGui.QColor(34, 139, 34)) # 将对应位置设置为绿色
elif new < old:
self.bg.item(h, l).setBackground(QtGui.QColor(227, 23, 13)) # 将最对应位置设置为红色
old = new # 更新旧标志
l += 1 # 列右移动
sleep(1.2)
我想通过一个按钮来在当前操作表格块的上方显示特定的文字,这就需要另一个函数获得table函数中行(h)和列(l)的实时值来确定位置,请问如何从类中的另一个函数获取本函数中变量的运行实时值?
def mark(self):
self.bg.setItem(h - 1, l, QTableWidgetItem('ok')) # 设置标志
能有简易代码更好,十分感谢!元宵节快乐!
可以将行(h)和列(l)作为类的成员变量,然后在 table 函数和另一个函数中共享它们的值。
例如:
class Widget(QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.h = 10
self.l = 0
def table(self):
old = update()
while True:
new = update()
if new > old:
self.bg.item(self.h, self.l).setBackground(QtGui.QColor(34, 139, 34)) # 将对应位置设置为绿色
elif new < old:
self.bg.item(self.h, self.l).setBackground(QtGui.QColor(227, 23, 13)) # 将最对应位置设置为红色
old = new # 更新旧标志
self.l += 1 # 列右移动
sleep(1.2)
def another_function(self):
# 获取 table 函数中行(h)和列(l)的实时值
position_text = f"Row: {self.h}, Column: {self.l}"
# 显示文字
self.text_label.setText(position_text)
你是说class A中有函数func_a和func_b,func_b中有局部变量func_b_varible,然后func_a想实时知道func_b_varible的值?可以在func_b退出前,将func_b_varible赋值给class A的成员变量,这样func_a去访问这个成员变量即可
不知道你这个问题是否已经解决, 如果还没有解决的话: