gpt4 book ai didi

python - Qlabels 在最后被剪掉

转载 作者:行者123 更新时间:2023-12-05 01:17:46 25 4
gpt4 key购买 nike

我正在尝试使用 PyQt 为一段 python 代码制作一个 GUI。但是,由于我不知道的原因,GUI 似乎切断了两个 Qlabel 的末端。我已经针对类似问题浏览了 Stack Overflow 上的其他问题,但没有找到针对我的特定问题的解决方案。

最初它工作正常。但在这一切之后,我在我的 GUI 中添加了一个状态栏,并且不得不将所有内容向下移动一些。在我移动了所有内容之后,文本开始从末尾被切断。我确实尝试过逆转这个过程,但没有帮助。

我附上输出的屏幕截图:

enter image description here

这是 GUI 的完整代码:

import sys, os
from nltk.corpus import stopwords
from nltk import FreqDist, word_tokenize, sent_tokenize
from string import punctuation
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets


class Window(QMainWindow, QWidget):

def __init__(self):
super(Window, self).__init__()
self.setGeometry(450, 450, 800, 500)
self.setWindowTitle('Text Summarizer')
self.setWindowIcon(QIcon('summarize.png'))
self.exit_text = 'Exited Application'
self.file_open_button = None
self.quit_button = None
self.textbox_input = None
self.textbox_output = None
self.output_label = None
self.input_label = None
self.file_text = None
self.my_output = None
self.my_text = None
self.summarize_button = None
self.n = None
self.no_lines = None
self.line_no_input = None
self.sent_no = None
self.warning_text_1 = 'You have chosen summarization length more than the actual text length!\n' + \
'Please enter a valid number less than the length of actual text'

self.home()


# Used to anchor each element to edges with some offset (padding)
def resizeEvent(self, event):
p = self.rect().bottomRight() - QPoint(20, 10) - self.quit_button.rect().bottomRight()
self.quit_button.move(p)
QWidget.resizeEvent(self, event)
p = self.rect().bottomLeft() - QPoint(-20, 10) - self.file_open_button.rect().bottomLeft()
self.file_open_button.move(p)
QWidget.resizeEvent(self, event)
p = self.rect().topRight() - QPoint(20, -60) - self.textbox_output.rect().topRight()
self.textbox_output.move(p)
QWidget.resizeEvent(self, event)
p = self.rect().topRight() - QPoint(135, -25) - self.output_label.rect().topRight()
self.output_label.move(p)
QWidget.resizeEvent(self, event)

# Open file Function
# Opens Dialog Box to open the file and select text
# If file is of valid readable format, displays the contents on the left text box
# Else throws a warning dialog box and prompts to either chose again else cancel
def file_open(self):
# noinspection PyBroadException
try:
name = QFileDialog.getOpenFileName(self, 'Open File')
file = open(name[0], 'r')
self.file_text = file.read()
self.sent_no = len(sent_tokenize(self.file_text))
self.textbox_input.setPlainText(self.file_text)

return self.file_text
except Exception as e:
print('Error Reported:', e)
message_box = QMessageBox.warning(self, 'Error!', 'File Open Error! Please Choose Valid File!',
QMessageBox.Ok | QMessageBox.Cancel)
if message_box == QMessageBox.Ok:
self.file_open()
else:
pass

# Takes the input from the left textbox, summarize that and display on the right text box
def summarize(self):
# noinspection PyBroadException
try:
self.n = self.line_no_input.text()
self.n = (int(self.n))
if self.n > self.sent_no:
QMessageBox.warning(self, 'Error!', self.warning_text_1,
QMessageBox.Ok)
else:
self.my_text = self.textbox_input.toPlainText()
self.my_output = self.text_summarize(self.my_text, no_lines=self.n)
self.textbox_output.setPlainText(self.my_output)
except Exception as e:
print(e)
QMessageBox.warning(self, 'Error!', 'You have to input the text in the input textbox!\n'
'You have to input a number in the number box!',
QMessageBox.Ok)

# main function containing all the buttons and other elements to display
def home(self):
# The exit button on the right bottom corner
self.quit_button = QPushButton('Quit', self)
self.quit_button.clicked.connect(self.exit_application)
self.quit_button.resize(self.quit_button.minimumSizeHint())

# The File Open button on the left bottom corner
self.file_open_button = QPushButton('Open File', self)
self.file_open_button.clicked.connect(self.file_open)
self.file_open_button.resize(self.file_open_button.minimumSizeHint())

# The Summarize button on the left bottom corner
self.summarize_button = QPushButton('Summarize', self)
self.summarize_button.clicked.connect(self.summarize)
self.summarize_button.resize(self.summarize_button.minimumSizeHint())
self.summarize_button.move(365, 280)

self.file_save_button = QPushButton('Save File', self)
self.file_save_button.clicked.connect(self.file_save)
self.file_save_button.resize(self.file_save_button.minimumSizeHint())
self.file_save_button.move(365, 400)

# Left Textbox element used to input the text to be summarized --- Editable
self.textbox_input = QPlainTextEdit(self)
self.textbox_input.move(20, 65)
self.textbox_input.resize(280, 395)

# Right Textbox element used to display the output of the summarized text --- Not Editable (incomplete)
self.textbox_output = QTextEdit(self)
self.textbox_output.setReadOnly(True)
self.textbox_output.move(340, 40)
self.textbox_output.resize(280, 400)

self.line_no_input = QLineEdit(self)
self.line_no_input.move(430, 323)
self.line_no_input.resize(30, 25)

# Left Textbox heading label
self.input_label = QLabel(self, text='Input Request')
new_font = QFont("Arial", 16, QFont.Bold)
self.input_label.setFont(new_font)
self.input_label.move(95, 25)

# Right Textbox heading label
self.output_label = QLabel(self, text='Output Text')
new_font = QFont("Arial", 16, QFont.Bold)
self.output_label.setFont(new_font)

self.no_lines = QLabel(self, text='Number of lines:')
new_font = QFont("Arial", 10)
self.no_lines.setFont(new_font)
self.no_lines.move(330, 320)

menu_open_file = QAction("&Open File", self)
menu_open_file.setShortcut("Ctrl+O")
menu_open_file.setStatusTip('Open from text file')
menu_open_file.triggered.connect(self.file_open)

menu_save_file = QAction("&Save File", self)
menu_save_file.setShortcut("Ctrl+S")
menu_save_file.setStatusTip('Save the out text')
menu_save_file.triggered.connect(self.file_save)

menu_exit = QAction("&Exit", self)
menu_exit.setShortcut("Ctrl+Q")
menu_exit.setStatusTip('Exit the program')
menu_exit.triggered.connect(self.exit_application)

self.statusBar()

main_menu = self.menuBar()
file_menu = main_menu.addMenu('&File')
file_menu.addAction(menu_open_file)
file_menu.addAction(menu_save_file)
file_menu.addAction(menu_exit)

self.show()

# Exit Definition. Runs when the app is Quit using the 'Quit' button
def exit_application(self):
print(self.exit_text)
sys.exit()

def selection_box(self):
print('Inside selection_box')
combo_box = QComboBox(self)
for i in range(self.sent_no):
item_text = str(i + 1) + ' Lines'
combo_box.addItem(item_text)
combo_box.move(365, 300)
qApp.processEvents()

def file_save(self):
try:
name = QFileDialog.getSaveFileName(self, 'Save File', '', '*.txt')
file = open(name[0], 'w')
text = self.textbox_output.toPlainText()
file.write(text)
file.close()
except Exception as e:
print(e)
QMessageBox.warning(self, 'Error!', "You don't have any text to save!",
QMessageBox.Ok)


if __name__ == '__main__':
app = QApplication(sys.argv)
GUI = Window()
# GUI.show()
sys.exit(app.exec_())

最佳答案

如果您希望 QLabel 适合文本,那么您必须使用 adjustSize(),在您的情况下:

self.input_label.adjustSize()
self.output_label.adjustSize()

之前:

enter image description here

之后:

enter image description here

关于python - Qlabels 在最后被剪掉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48665788/

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