質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Qt Creator

Qt Creatorは、Qtアプリケーションを開発するための開発統合環境(IDE)です。

Q&A

解決済

1回答

4495閲覧

Raspberry Pi 3 PyQt5 を用いたGUIにて、PushButtonの背景色が変更できない

BluePi85

総合スコア22

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Qt Creator

Qt Creatorは、Qtアプリケーションを開発するための開発統合環境(IDE)です。

0グッド

0クリップ

投稿2020/03/17 12:34

前提・実現したいこと

RaspberryPi3 Python3.5.3 Qt5にて、PushButtonを押下際に、
PushButtonの文字色・背景色を変更したいです。

発生している問題・エラーメッセージ

Windows10 Python3.6.8 Qt5.12.2にて、同一のプログラムを作成したところ、意図した動作を確認出来、
同一ファイルをRaspberryPiで確認したところ、文字色のみ変更出来ましたが、背景色は変更出来ませんでした。

該当のソースコード

GUI はQt Designerにて作成

QtDesigner

1<?xml version="1.0" encoding="UTF-8"?> 2<ui version="4.0"> 3 <class>Form</class> 4 <widget class="QWidget" name="Form"> 5 <property name="geometry"> 6 <rect> 7 <x>0</x> 8 <y>0</y> 9 <width>429</width> 10 <height>323</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>Form</string> 15 </property> 16 <widget class="QLabel" name="label"> 17 <property name="geometry"> 18 <rect> 19 <x>40</x> 20 <y>40</y> 21 <width>140</width> 22 <height>30</height> 23 </rect> 24 </property> 25 <property name="minimumSize"> 26 <size> 27 <width>140</width> 28 <height>30</height> 29 </size> 30 </property> 31 <property name="maximumSize"> 32 <size> 33 <width>140</width> 34 <height>30</height> 35 </size> 36 </property> 37 <property name="font"> 38 <font> 39 <pointsize>14</pointsize> 40 </font> 41 </property> 42 <property name="text"> 43 <string>TextLabel</string> 44 </property> 45 </widget> 46 <widget class="QRadioButton" name="radioButton"> 47 <property name="geometry"> 48 <rect> 49 <x>40</x> 50 <y>100</y> 51 <width>140</width> 52 <height>30</height> 53 </rect> 54 </property> 55 <property name="minimumSize"> 56 <size> 57 <width>140</width> 58 <height>30</height> 59 </size> 60 </property> 61 <property name="maximumSize"> 62 <size> 63 <width>140</width> 64 <height>30</height> 65 </size> 66 </property> 67 <property name="font"> 68 <font> 69 <pointsize>14</pointsize> 70 </font> 71 </property> 72 <property name="text"> 73 <string>RadioButton</string> 74 </property> 75 </widget> 76 <widget class="QCheckBox" name="checkBox"> 77 <property name="geometry"> 78 <rect> 79 <x>40</x> 80 <y>170</y> 81 <width>140</width> 82 <height>30</height> 83 </rect> 84 </property> 85 <property name="minimumSize"> 86 <size> 87 <width>140</width> 88 <height>30</height> 89 </size> 90 </property> 91 <property name="maximumSize"> 92 <size> 93 <width>140</width> 94 <height>30</height> 95 </size> 96 </property> 97 <property name="font"> 98 <font> 99 <pointsize>14</pointsize> 100 </font> 101 </property> 102 <property name="text"> 103 <string>CheckBox</string> 104 </property> 105 </widget> 106 <widget class="QPushButton" name="pushButton"> 107 <property name="geometry"> 108 <rect> 109 <x>160</x> 110 <y>230</y> 111 <width>150</width> 112 <height>35</height> 113 </rect> 114 </property> 115 <property name="minimumSize"> 116 <size> 117 <width>150</width> 118 <height>35</height> 119 </size> 120 </property> 121 <property name="maximumSize"> 122 <size> 123 <width>150</width> 124 <height>35</height> 125 </size> 126 </property> 127 <property name="font"> 128 <font> 129 <pointsize>14</pointsize> 130 </font> 131 </property> 132 <property name="text"> 133 <string>PushButton</string> 134 </property> 135 </widget> 136 </widget> 137 <resources/> 138 <connections> 139 <connection> 140 <sender>pushButton</sender> 141 <signal>clicked()</signal> 142 <receiver>Form</receiver> 143 <slot>myFunc()</slot> 144 <hints> 145 <hint type="sourcelabel"> 146 <x>230</x> 147 <y>242</y> 148 </hint> 149 <hint type="destinationlabel"> 150 <x>352</x> 151 <y>206</y> 152 </hint> 153 </hints> 154 </connection> 155 </connections> 156 <slots> 157 <slot>myFunc()</slot> 158 </slots> 159</ui> 160

GUI はQt Designerにて作成した.uiファイルをpythonに変換

python

1# -*- coding: utf-8 -*- 2 3# Form implementation generated from reading ui file '.\GUI_Color.ui' 4# 5# Created by: PyQt5 UI code generator 5.12.2 6# 7# WARNING! All changes made in this file will be lost! 8 9from PyQt5 import QtCore, QtGui, QtWidgets 10 11 12class Ui_Form(object): 13 def setupUi(self, Form): 14 Form.setObjectName("Form") 15 Form.resize(429, 323) 16 self.label = QtWidgets.QLabel(Form) 17 self.label.setGeometry(QtCore.QRect(40, 40, 140, 30)) 18 self.label.setMinimumSize(QtCore.QSize(140, 30)) 19 self.label.setMaximumSize(QtCore.QSize(140, 30)) 20 font = QtGui.QFont() 21 font.setPointSize(14) 22 self.label.setFont(font) 23 self.label.setObjectName("label") 24 self.radioButton = QtWidgets.QRadioButton(Form) 25 self.radioButton.setGeometry(QtCore.QRect(40, 100, 140, 30)) 26 self.radioButton.setMinimumSize(QtCore.QSize(140, 30)) 27 self.radioButton.setMaximumSize(QtCore.QSize(140, 30)) 28 font = QtGui.QFont() 29 font.setPointSize(14) 30 self.radioButton.setFont(font) 31 self.radioButton.setObjectName("radioButton") 32 self.checkBox = QtWidgets.QCheckBox(Form) 33 self.checkBox.setGeometry(QtCore.QRect(40, 170, 140, 30)) 34 self.checkBox.setMinimumSize(QtCore.QSize(140, 30)) 35 self.checkBox.setMaximumSize(QtCore.QSize(140, 30)) 36 font = QtGui.QFont() 37 font.setPointSize(14) 38 self.checkBox.setFont(font) 39 self.checkBox.setObjectName("checkBox") 40 self.pushButton = QtWidgets.QPushButton(Form) 41 self.pushButton.setGeometry(QtCore.QRect(160, 230, 150, 35)) 42 self.pushButton.setMinimumSize(QtCore.QSize(150, 35)) 43 self.pushButton.setMaximumSize(QtCore.QSize(150, 35)) 44 font = QtGui.QFont() 45 font.setPointSize(14) 46 self.pushButton.setFont(font) 47 self.pushButton.setObjectName("pushButton") 48 49 self.retranslateUi(Form) 50 self.pushButton.clicked.connect(Form.myFunc) 51 QtCore.QMetaObject.connectSlotsByName(Form) 52 53 def retranslateUi(self, Form): 54 _translate = QtCore.QCoreApplication.translate 55 Form.setWindowTitle(_translate("Form", "Form")) 56 self.label.setText(_translate("Form", "TextLabel")) 57 self.radioButton.setText(_translate("Form", "RadioButton")) 58 self.checkBox.setText(_translate("Form", "CheckBox")) 59 self.pushButton.setText(_translate("Form", "PushButton")) 60 61 62 63 64if __name__ == "__main__": 65 import sys 66 app = QtWidgets.QApplication(sys.argv) 67 Form = QtWidgets.QWidget() 68 ui = Ui_Form() 69 ui.setupUi(Form) 70 Form.show() 71 sys.exit(app.exec_()) 72 73

GUIを起動する

python

1import os 2import sys 3 4from PyQt5 import QtCore as QC 5from PyQt5 import QtGui as Qg 6from PyQt5 import QtWidgets as Qw 7 8import GUI_Color 9 10class MyForm(Qw.QMainWindow): 11 def __init__(self, parent=None): 12 super().__init__(parent) 13 self.ui = GUI_Color.Ui_Form() 14 self.ui.setupUi(self) 15 16 def myFunc(self): 17 # setStyleSheet("QLabel {color: #cccccc;}") 18 # label 19 self.ui.label.setStyleSheet('QLabel { background-color : red; color : green; }') 20 # radioButton 21 self.ui.radioButton.setStyleSheet('QRadioButton { background-color : blue; color : red; }') 22 # checkBox 23 self.ui.checkBox.setStyleSheet('QCheckBox {background-color : green; color : blue; }') 24 # pushButton 25 self.ui.pushButton.setStyleSheet('QPushButton {background-color : yellow; color : red; }') 26 27if __name__ == "__main__": 28 app = Qw.QApplication(sys.argv) 29 wmain = MyForm() 30 wmain.show() 31 app.exec_() 32

試したこと

初期画面 @ Windows
イメージ説明

PushButton押下後 @ Windows
イメージ説明

初期画面 @ RaspberryPi
イメージ説明

PushButton押下後 @ RaspberryPi
イメージ説明

どのように行えば、Windowsのようになるのかアドバイスを頂けないでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

BluePi85さん、

2020-02-13-raspbian-busterベースの環境(Python 3.7.3)ですが、上記の現象を確認しました.
また、以下のように、border: none;を追加することで、ボタンの背景が黄色になりました.

self.ui.pushButton.setStyleSheet('QPushButton {background-color: yellow; color: red; border: none;}')
  • 実行例: (右が、border: none; あり)

イメージ説明

# 実行環境 - Raspberry Pi 4B - Raspbian: 2020-02-13-raspbian-buster ## PyQt5インストール sudo apt update && sudo apt upgrade -y && sudo apt install -y qt5-default qtcreator python3-pyqt5

投稿2020/03/18 21:15

mt08

総合スコア1825

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

BluePi85

2020/03/19 12:49

ご連絡ありがとうございます。 Raspberry Pi 3B Raspbian 2019/4/8 NOOBS 3.0.1 Stretch の環境下にて 動作確認を取る事が出来ました。 この度はありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問