Почему не срабатывает def on_change(не вставляется картинка)?
Почему не срабатывает def on_change (не вставляется картинка) ?
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)
self.background_layout = QHBoxLayout()
root_layout.addLayout(self.background_layout)
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)
self.combobox.currentIndexChanged.connect(self.on_change)
def on_change(self, index):
if index == 0:
# Не работает
label = QLabel()
pixmap = QPixmap('./temp/contrast.png')
label.setPixmap(pixmap)
self.background_layout.addWidget(label)
label.show()
def post_background(self):
background = self.combobox.currentText()
if background == 'Contrast':
self.background_top = 'white'
self.color_top = self.background_bottom
self.color_bottom = self.background_top
self.background_bottom = 'black'
elif background == 'Monako':
self.background_top = 'black'
self.color_top = 'white'
self.background_bottom = 'yellow'
self.color_bottom = 'black'
self.close()
return self.background_top, self.background_bottom, self.color_top, self.color_bottom
Ответы (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.combobox.activated.emit(0) # +++
# 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.activated.connect(self.on_change) # +++
self.combobox.addItem('Contrast')
self.combobox.addItem('Monako')
root_layout.addWidget(self.combobox)
self.background_layout = QHBoxLayout()
root_layout.addLayout(self.background_layout)
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)
# self.combobox.currentIndexChanged.connect(self.on_change) # ---
def on_change(self, index):
if index == 0:
# Не работает
label = QLabel()
pixmap = QPixmap('Ok.png') #('./temp/contrast.png')
label.setPixmap(pixmap)
self.background_layout.addWidget(label)
label.show()
def post_background(self):
background = self.combobox.currentText()
if background == 'Contrast':
self.background_top = 'white'
self.color_top = self.background_bottom
self.color_bottom = self.background_top
self.background_bottom = 'black'
elif background == 'Monako':
self.background_top = 'black'
self.color_top = 'white'
self.background_bottom = 'yellow'
self.color_bottom = 'black'
self.close()
return self.background_top, self.background_bottom, self.color_top, self.color_bottom
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Background()
w.show()
sys.exit(app.exec_())
