PyQt5 QTextBrowser - setText - 对齐问题? [英] PyQt5 QTextBrowser - setText - Alignment issue?
问题描述
我正在尝试使用 PyQt5 制作一个简单的 GUI 控制台.在尝试使用 QTextBrowser - setText 打印文本时,它会失去对齐并且看起来很糟糕.但文本在我的 python 控制台中对齐
我正在使用 setText 函数来显示我的数据框.更改 df.to_string() 的 justify 参数后,我可以在 python 控制台中看到更改的对齐方式,但这并未反映在我的 Qt 控制台中.
代码:
导入系统从 GUI_4 导入 Ui_MainWindow从 PyQt5 导入 QtCore、QtGui、QtWidgets导入 New_Read_Map_File定义窗口():app = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QWidget()标签 = QtWidgets.QTextBrowser(MainWindow)label.setStyleSheet('颜色:蓝色')MainWindow.setGeometry(600,150,800,800)label.setGeometry(10,10,780,780)获取数据()label.setText(DisplayData)MainWindow.show()sys.exit(app.exec_())定义获取数据():全局显示数据New_Read_Map_File.read_MapFile_main()DisplayData = (New_Read_Map_File.df.to_string(col_space = 14,justify = "justify"))打印(显示数据)窗户()
预期对齐
观察到的 Qt GUI
问题是字体引起的,在控制台和很多IDES使用
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
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_())
这篇关于PyQt5 QTextBrowser - setText - 对齐问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!