PYQT5 QTEXTBROWSER- setText-对齐问题?

发布于 2025-01-30 02:36:17 字数 1126 浏览 3 评论 0原文

我正在尝试使用PYQT5制作一个简单的GUI控制台。在尝试使用QTEXTBROWSER -SetText打印文本时,它会失去对齐方式,并且看起来不好。但是文本在我的python控制台中对齐,

我正在使用setText函数显示我的数据框架。 在更改df.to_string()的Jusify参数时,我可以看到Python控制台中的更改对齐,但这并未反映在我的QT控制台中。

代码:

import sys
from GUI_4 import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
import New_Read_Map_File

def window():    
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QWidget()   
    label = QtWidgets.QTextBrowser(MainWindow)                    
    label.setStyleSheet('color: blue')    
    MainWindow.setGeometry(600,150,800,800)
    label.setGeometry(10,10,780,780)    
    GetData()
    label.setText(DisplayData)
    MainWindow.show()
    sys.exit(app.exec_())    

def GetData():
    global DisplayData
    New_Read_Map_File.read_MapFile_main()
    DisplayData = (New_Read_Map_File.df.to_string(col_space = 14,justify = "justify"))    
    print(DisplayData)


window()

预期对齐

“”

观察到的qt gui

“”

I am trying to make a simple GUI console using PyQt5. On trying to print the text using QTextBrowser - setText, it loses alignment and looks bad. but the text is aligned in my python console

I am using the setText function to display my data frame.
On changing the justify parameter of df.to_string(), i am able to see the changed alignment in the python console, but this is not reflected in my Qt console.

Code :

import sys
from GUI_4 import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
import New_Read_Map_File

def window():    
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QWidget()   
    label = QtWidgets.QTextBrowser(MainWindow)                    
    label.setStyleSheet('color: blue')    
    MainWindow.setGeometry(600,150,800,800)
    label.setGeometry(10,10,780,780)    
    GetData()
    label.setText(DisplayData)
    MainWindow.show()
    sys.exit(app.exec_())    

def GetData():
    global DisplayData
    New_Read_Map_File.read_MapFile_main()
    DisplayData = (New_Read_Map_File.df.to_string(col_space = 14,justify = "justify"))    
    print(DisplayData)


window()

Expected Alignment

Observed Qt GUI

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙厌世 2025-02-06 02:36:17

问题是由字体引起的,在控制台的情况下,许多IDE都使用a

例如,如果使用单拼字体:

import numpy as np
import pandas as pd
from PyQt5 import QtCore, QtGui, QtWidgets

def pandas_to_str():
    df = pd.DataFrame({ 
        'A' : 1.,
        'B' : pd.Timestamp('20130102'),
        'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
        'D' : np.array([3] * 4,dtype='int32'),
        'E' : pd.Categorical(["test","train","test","train"]),
        'F' : 'foo' })
    return df.to_string(col_space =14,justify = "justify")

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QTextBrowser()
    w.setStyleSheet('color: blue') 
    w.setFont(QtGui.QFont("Monospace"))
    w.setWordWrapMode(QtGui.QTextOption.NoWrap)
    w.setText(pandas_to_str())
    w.showMaximized()
    sys.exit(app.exec_())

”在此处输入图像描述

The problem is caused by the font, in the case of consoles and many IDES use a monospaced font.

For example, if you use the Monospace font:

import numpy as np
import pandas as pd
from PyQt5 import QtCore, QtGui, QtWidgets

def pandas_to_str():
    df = pd.DataFrame({ 
        'A' : 1.,
        'B' : pd.Timestamp('20130102'),
        'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
        'D' : np.array([3] * 4,dtype='int32'),
        'E' : pd.Categorical(["test","train","test","train"]),
        'F' : 'foo' })
    return df.to_string(col_space =14,justify = "justify")

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QTextBrowser()
    w.setStyleSheet('color: blue') 
    w.setFont(QtGui.QFont("Monospace"))
    w.setWordWrapMode(QtGui.QTextOption.NoWrap)
    w.setText(pandas_to_str())
    w.showMaximized()
    sys.exit(app.exec_())

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文