Как мне перетянуть данные полученные с класса Background функции post_background в класс Window(а точнее в функцию get_background)?
Как мне перетянуть данные полученные с класса Background функции post_background в класс Window (а точнее в функцию get_background)?
class Background(QDialog):
background_bottom = ''
background_top = ''
def __init__(self):
super().__init__()
self.width = 400
self.height = 400
self.title = 'Background'
self.init_window()
self.center()
self.init_ui()
self.show()
def init_window(self):
self.setGeometry(0, 0, self.width, self.height)
self.setWindowTitle(self.title)
def center(self):
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
def init_ui(self):
root_layout = QVBoxLayout()
self.combobox = QComboBox()
self.combobox.addItem('Contrast')
self.combobox.addItem('Monako')
root_layout.addWidget(self.combobox)
background_label = QLabel('background')
background_label.setAlignment(Qt.AlignCenter)
root_layout.addWidget(background_label)
buttons_layout = QHBoxLayout()
buttons_layout.setAlignment(Qt.AlignRight)
button_ok = QPushButton('Ok')
button_ok.clicked.connect(self.post_background)
button_cancel = QPushButton('Cancel')
button_cancel.clicked.connect(self.cancel)
buttons_layout.addWidget(button_ok)
buttons_layout.addWidget(button_cancel)
root_layout.addLayout(buttons_layout)
self.setLayout(root_layout)
def post_background(self):
background = self.combobox.currentText()
if background == 'Contrast':
self.background_top = 'white'
self.background_bottom = 'black'
elif background == 'Monako':
self.background_top = 'black'
self.background_bottom = 'yellow'
self.close()
return self.background_top, self.background_bottom
def cancel(self):
self.close()
class Window(QMainWindow):
buffered_text = ''
current_text = ''
count = 1
text_after = ''
text_before = ''
text_copy = ''
isSaved = False
def __init__(self):
super().__init__()
self.top = 150
self.left = 150
self.height = 600
self.width = 1200
self.default = 'Simple'
self.title = f'{self.default}-Notebook'
self.init_ui()
self.init_window_1()
self.show()
def init_ui(self):
self.textbox = QTextEdit()
self.textbox.setFocusPolicy(Qt.StrongFocus)
self.textbox.setFont(QFont('Times', 18))
self.textbox.selectionChanged.connect(self.handleSelectionChanged)
self.cursor = self.textbox.textCursor()
bar = self.menuBar()
file_menu = bar.addMenu('File')
create_action = QAction('Create', self)
create_action.setShortcut('CTRL+N')
open_action = QAction('Open...', self)
open_action.setShortcut('CTRL+O')
save_action = QAction('Save', self)
save_action.setShortcut('CTRL+S')
save_as_action = QAction('Save as...', self)
save_as_action.setShortcut('CTRL+SHIFT+S')
close_action = QAction('Close', self)
file_menu.addAction(create_action)
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addAction(save_as_action)
file_menu.addSeparator()
file_menu.addAction(close_action)
create_action.triggered.connect(self.create)
open_action.triggered.connect(self.open)
save_action.triggered.connect(self.save)
save_as_action.triggered.connect(self.save_as)
close_action.triggered.connect(self.close_app)
file_edit = bar.addMenu('Edit')
cancel_action = QAction('Cancel', self)
cancel_action.setShortcut('CTRL+Z')
cut_action = QAction('Cut', self)
cut_action.setShortcut('CTRL+X')
copy_action = QAction('Copy', self)
copy_action.setShortcut('CTRL+C')
paste_action = QAction('Paste', self)
paste_action.setShortcut('CTRL+V')
delete_action = QAction('Delete', self)
delete_action.setShortcut('DELETE')
file_edit.addAction(cancel_action)
file_edit.addSeparator()
file_edit.addAction(cut_action)
file_edit.addAction(copy_action)
file_edit.addAction(paste_action)
file_edit.addAction(delete_action)
cancel_action.triggered.connect(self.cancel)
cut_action.triggered.connect(self.cut)
copy_action.triggered.connect(self.copy)
paste_action.triggered.connect(self.paste)
delete_action.triggered.connect(self.delete)
file_format = bar.addMenu('Format')
font_action = QAction('Fonts...', self)
color_action = QAction('Color', self)
background_action = QAction('Background', self)
file_format.addAction(font_action)
file_format.addAction(color_action)
file_format.addAction(background_action)
font_action.triggered.connect(self.get_fonts)
color_action.triggered.connect(self.get_color)
background_action.triggered.connect(self.get_background)
self.setCentralWidget(self.textbox)
def get_background(self):
Background().exec_()
background = Background.post_background()
print(background) # не то!!!!
def get_fonts(self):
font, ok = QFontDialog.getFont()
if ok:
self.textbox.setFont(font)
def get_color(self):
color = QColorDialog.getColor()
self.textbox.setTextColor(color)
def init_window_1(self):
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowTitle(self.title)
self.setWindowIcon(QIcon('../Icon/Windows_Notepad_Icon.png'))
def handleSelectionChanged(self):
text = self.textbox.textCursor().selectedText()
text = '\n'.join(text.split())
return text
def cancel(self):
print(self.buffered_text)
print(self.current_text)
self.current_text = self.textbox.toPlainText()
self.textbox.setText(self.buffered_text)
self.cursor.setPosition(len(self.buffered_text), QTextCursor.KeepAnchor)
self.textbox.setTextCursor(self.cursor)
self.buffered_text = self.current_text
def cut(self):
text = self.handleSelectionChanged()
self.current_text = self.textbox.toPlainText()
# print(type(self.current_text))
print(f'current_text = \n`{self.current_text}`\n')
self.buffered_text = self.current_text
if text in self.current_text:
new_text = self.current_text.replace(text, '')
self.textbox.setText(new_text)
self.cursor.setPosition(len(new_text), QTextCursor.KeepAnchor)
self.textbox.setTextCursor(self.cursor)
def copy(self):
self.text_copy = self.textbox.toPlainText()
def paste(self):
self.textbox.setText(self.text_copy)
def delete(self):
self.cut()
def save_as(self):
file_name, _ = QFileDialog.getSaveFileName(self, 'Save', '*.txt', "Текстовые документы (*.txt);;"
"All Files()")
if file_name:
with open(file_name, 'w+') as file:
file.write(self.textbox.toPlainText())
def save(self):
self.text_before = self.textbox.toPlainText()
if self.text_before:
if self.text_before != self.text_after:
self.save_as()
self.isSaved = True
self.text_after = self.text_before
def open(self):
file_name, file_extension = QFileDialog.getOpenFileName(self, 'Open file...', 'C://Program Files/')
if file_name:
with open(file_name, 'r') as file:
self.textbox.setText(file.read())
def create(self):
text = self.textbox.toPlainText()
if text:
change = QMessageBox.question(self, 'Notebook', 'Do you want to save the changes to a file?',
QMessageBox.Save | QMessageBox.No | QMessageBox.Cancel)
if change == QMessageBox.Save:
self.save_as()
elif change == QMessageBox.No:
self.textbox.clear()
def close_app(self):
text = self.textbox.toPlainText()
if text:
if not self.isSaved:
change = QMessageBox.question(self, 'Notebook', 'Do you want to save the changes to a file?',
QMessageBox.Save | QMessageBox.No | QMessageBox.Cancel)
if change == QMessageBox.Save:
self.save_as()
elif change == QMessageBox.No:
sys.exit()
else:
return True
def closeEvent(self, event):
if self.close_app():
event.ignore()
else:
self.close_app()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
я отметил для вас строки, который я поменял:
import sys
from PyQt5.Qt import *
class Background(QDialog):
background_bottom = ''
background_top = ''
def __init__(self):
super().__init__()
self.width = 400
self.height = 400
self.title = 'Background'
self.init_window()
self.center()
self.init_ui()
self.show()
def init_window(self):
self.setGeometry(0, 0, self.width, self.height)
self.setWindowTitle(self.title)
def center(self):
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
def init_ui(self):
root_layout = QVBoxLayout()
self.combobox = QComboBox()
self.combobox.addItem('Contrast')
self.combobox.addItem('Monako')
root_layout.addWidget(self.combobox)
background_label = QLabel('background')
background_label.setAlignment(Qt.AlignCenter)
root_layout.addWidget(background_label)
buttons_layout = QHBoxLayout()
buttons_layout.setAlignment(Qt.AlignRight)
button_ok = QPushButton('Ok')
button_ok.clicked.connect(self.post_background)
button_cancel = QPushButton('Cancel')
button_cancel.clicked.connect(self.cancel)
buttons_layout.addWidget(button_ok)
buttons_layout.addWidget(button_cancel)
root_layout.addLayout(buttons_layout)
self.setLayout(root_layout)
# >>
def post_background(self):
background = self.combobox.currentText()
if background == 'Contrast':
self.background_top = 'white'
self.background_bottom = 'black'
elif background == 'Monako':
self.background_top = 'black'
self.background_bottom = 'yellow'
self.close()
#print(f'def post_background(self): \n{self.background_top}, \n{self.background_bottom}')
return self.background_top, self.background_bottom
def cancel(self):
self.close()
class Window(QMainWindow):
buffered_text = ''
current_text = ''
count = 1
text_after = ''
text_before = ''
text_copy = ''
isSaved = False
def __init__(self):
super().__init__()
self.top = 150
self.left = 150
self.height = 600
self.width = 1200
self.default = 'Simple'
self.title = f'{self.default}-Notebook'
self.init_ui()
self.init_window_1()
self.show()
def init_ui(self):
self.textbox = QTextEdit()
self.textbox.setFocusPolicy(Qt.StrongFocus)
self.textbox.setFont(QFont('Times', 18))
self.textbox.selectionChanged.connect(self.handleSelectionChanged)
self.cursor = self.textbox.textCursor()
bar = self.menuBar()
file_menu = bar.addMenu('File')
create_action = QAction('Create', self)
create_action.setShortcut('CTRL+N')
open_action = QAction('Open...', self)
open_action.setShortcut('CTRL+O')
save_action = QAction('Save', self)
save_action.setShortcut('CTRL+S')
save_as_action = QAction('Save as...', self)
save_as_action.setShortcut('CTRL+SHIFT+S')
close_action = QAction('Close', self)
file_menu.addAction(create_action)
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addAction(save_as_action)
file_menu.addSeparator()
file_menu.addAction(close_action)
create_action.triggered.connect(self.create)
open_action.triggered.connect(self.open)
save_action.triggered.connect(self.save)
save_as_action.triggered.connect(self.save_as)
close_action.triggered.connect(self.close_app)
file_edit = bar.addMenu('Edit')
cancel_action = QAction('Cancel', self)
cancel_action.setShortcut('CTRL+Z')
cut_action = QAction('Cut', self)
cut_action.setShortcut('CTRL+X')
copy_action = QAction('Copy', self)
copy_action.setShortcut('CTRL+C')
paste_action = QAction('Paste', self)
paste_action.setShortcut('CTRL+V')
delete_action = QAction('Delete', self)
delete_action.setShortcut('DELETE')
file_edit.addAction(cancel_action)
file_edit.addSeparator()
file_edit.addAction(cut_action)
file_edit.addAction(copy_action)
file_edit.addAction(paste_action)
file_edit.addAction(delete_action)
cancel_action.triggered.connect(self.cancel)
cut_action.triggered.connect(self.cut)
copy_action.triggered.connect(self.copy)
paste_action.triggered.connect(self.paste)
delete_action.triggered.connect(self.delete)
file_format = bar.addMenu('Format')
font_action = QAction('Fonts...', self)
color_action = QAction('Color', self)
background_action = QAction('Background', self)
file_format.addAction(font_action)
file_format.addAction(color_action)
file_format.addAction(background_action)
font_action.triggered.connect(self.get_fonts)
color_action.triggered.connect(self.get_color)
background_action.triggered.connect(self.get_background)
self.setCentralWidget(self.textbox)
def get_background(self):
# Background().exec_()
# background = Background.post_background()
self.background = Background() # +++
self.background.exec_() # +++
background = self.background.post_background() # +++
print(f' background_top = {background[0]} \nself.background_bottom = {background[1]}') # не то!!!!
def get_fonts(self):
font, ok = QFontDialog.getFont()
if ok:
self.textbox.setFont(font)
def get_color(self):
color = QColorDialog.getColor()
self.textbox.setTextColor(color)
def init_window_1(self):
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowTitle(self.title)
self.setWindowIcon(QIcon('../Icon/Windows_Notepad_Icon.png'))
def handleSelectionChanged(self):
text = self.textbox.textCursor().selectedText()
text = '\n'.join(text.split())
return text
def cancel(self):
print(self.buffered_text)
print(self.current_text)
self.current_text = self.textbox.toPlainText()
self.textbox.setText(self.buffered_text)
self.cursor.setPosition(len(self.buffered_text), QTextCursor.KeepAnchor)
self.textbox.setTextCursor(self.cursor)
self.buffered_text = self.current_text
def cut(self):
text = self.handleSelectionChanged()
self.current_text = self.textbox.toPlainText()
# print(type(self.current_text))
print(f'current_text = \n`{self.current_text}`\n')
self.buffered_text = self.current_text
if text in self.current_text:
new_text = self.current_text.replace(text, '')
self.textbox.setText(new_text)
self.cursor.setPosition(len(new_text), QTextCursor.KeepAnchor)
self.textbox.setTextCursor(self.cursor)
def copy(self):
self.text_copy = self.textbox.toPlainText()
def paste(self):
self.textbox.setText(self.text_copy)
def delete(self):
self.cut()
def save_as(self):
file_name, _ = QFileDialog.getSaveFileName(self, 'Save', '*.txt', "Текстовые документы (*.txt);;"
"All Files()")
if file_name:
with open(file_name, 'w+') as file:
file.write(self.textbox.toPlainText())
def save(self):
self.text_before = self.textbox.toPlainText()
if self.text_before:
if self.text_before != self.text_after:
self.save_as()
self.isSaved = True
self.text_after = self.text_before
def open(self):
file_name, file_extension = QFileDialog.getOpenFileName(self, 'Open file...', 'C://Program Files/')
if file_name:
with open(file_name, 'r') as file:
self.textbox.setText(file.read())
def create(self):
text = self.textbox.toPlainText()
if text:
change = QMessageBox.question(self, 'Notebook', 'Do you want to save the changes to a file?',
QMessageBox.Save | QMessageBox.No | QMessageBox.Cancel)
if change == QMessageBox.Save:
self.save_as()
elif change == QMessageBox.No:
self.textbox.clear()
def close_app(self):
text = self.textbox.toPlainText()
if text:
if not self.isSaved:
change = QMessageBox.question(self, 'Notebook', 'Do you want to save the changes to a file?',
QMessageBox.Save | QMessageBox.No | QMessageBox.Cancel)
if change == QMessageBox.Save:
self.save_as()
elif change == QMessageBox.No:
sys.exit()
else:
return True
def closeEvent(self, event):
if self.close_app():
event.ignore()
else:
self.close_app()
if __name__ == '__main__':
app = QApplication(sys.argv)
mywin = Window()
mywin.show()
sys.exit(app.exec_())
