Es kam bei mir jetzt vor, dass als Wert bei jeder von 2 ComboBoxen der gleiche ausgewählt werden kann, aber nicht sollte. Ich habe mir dazu etwas programmiert, was den Wert bei Gleichheit auf den jeweiligen Ausgangswert zurücksetzt.
Geht dasselbe Verhalten auch einfacher mit einer entsprechenden Methode von Qt anstatt meinem Code unten?
main.py
Code: Alles auswählen
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
loadUi("color.ui", self)
self.background_color = self.comboBox_background_color.currentText()
self.foreground_color = self.comboBox_foreground_color.currentText()
self.comboBox_background_color.currentIndexChanged.connect(lambda: self.check_color("b"))
self.comboBox_foreground_color.currentIndexChanged.connect(lambda: self.check_color("f"))
def check_color(self, position):
if self.comboBox_background_color.currentText() == self.comboBox_foreground_color.currentText():
QMessageBox.warning(self, "Warnung", "Hintergrundfarbe und Vordergrundfarbe sollten nicht gleich sein.")
if position == "b":
self.comboBox_background_color.setCurrentText(self.background_color)
self.background_color = self.comboBox_background_color.currentText()
elif position == "f":
self.comboBox_foreground_color.setCurrentText(self.foreground_color)
self.foreground_color = self.comboBox_foreground_color.currentText()
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
color.ui
Code: Alles auswählen
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>260</x>
<y>80</y>
<width>287</width>
<height>71</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_background_color">
<property name="text">
<string>Hintergrundfarbe:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBox_background_color">
<item>
<property name="text">
<string>Rot</string>
</property>
</item>
<item>
<property name="text">
<string>Blau</string>
</property>
</item>
<item>
<property name="text">
<string>Grün</string>
</property>
</item>
<item>
<property name="text">
<string>Gelb</string>
</property>
</item>
<item>
<property name="text">
<string>Schwarz</string>
</property>
</item>
<item>
<property name="text">
<string>Weiß</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_foreground_color">
<property name="text">
<string>Vordergrundfarbe:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox_foreground_color">
<item>
<property name="text">
<string>Blau</string>
</property>
</item>
<item>
<property name="text">
<string>Rot</string>
</property>
</item>
<item>
<property name="text">
<string>Grün</string>
</property>
</item>
<item>
<property name="text">
<string>Gelb</string>
</property>
</item>
<item>
<property name="text">
<string>Schwarz</string>
</property>
</item>
<item>
<property name="text">
<string>Weiß</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>28</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Atalanttore