我正在尝试使用PyQt为一段python代码创建一个GUI。 然而,由于我不知道的原因,GUI似乎切断了两个Qlabels的末尾。我已经解决了类似问题的Stack Overflow上的其他问题,但我找不到针对我的具体问题的修复。
最初它工作正常。但是在一切之后,我在GUI中添加了一个状态栏,并且必须将所有内容都降低一些。在我移动了所有内容后,文本开始从最后切断。我确实试图扭转过程,但没有帮助。
我附上了输出的截图:
以下是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_())
注意: 我删除了文本摘要代码的部分内容,因为这是保密的。我很确定这与被删除的文本无关,因为这纯粹是一个后端程序。此外,窗口中间有一个公司徽标(png文件),但我已将其删除以保持机密性。