将DataFrame的结果输出到QLabel

问题描述 投票:1回答:1

我有一个在Python控制台中正确输出的Pandas DataFrame,但是我正在使用QT设计器创建一个应用程序,并且希望将此DataFrame输出到QLabel上,标签称为:

<widget class="QLabel" name="LBL_RESULT">. 

我需要使用其他窗口小部件类型来显示DataFrame吗?

我的代码是:

df=pd.DataFrame({"Identifier":id,"Description":desc})
print(df.to_string()) # prints everything to screen, not just the first page and last page.
df.to_csv('Test.txt',index=False) # saves the DataFrame to a text file
self.LBL_RESULT.setText(df)

错误是:

self.LBL_RESULT.setText(df)
TypeError: setText(self, str): argument 1 has unexpected type 'DataFrame'

请您提供协助。谢谢。

python python-3.x pandas pyqt qlabel
1个回答
1
投票
正如错误所指出,QLabel不希望数据帧而是字符串,因此您必须传递to_string()的结果:

df = pd.DataFrame({"Identifier":id,"Description":desc}) df.to_csv('Test.txt',index=False) # saves the DataFrame to a text file self.LBL_RESULT.setText(df.to_string()) self.LBL_RESULT.adjustSize()

© www.soinside.com 2019 - 2024. All rights reserved.