Python 3 – pyqtラジオボタン-Pythonチュートリアル

Python

PyQT QRadioButton シンプルなラジオボタンです。 これは通常、チェックボックスではなく、1つのオプションしか使用できない場合に使用されます。

qtでは、チェックボックスには常に丸いボタンと次のようなラベルがあります QRadioButton("Australia")

pyqtラジオボタンQRadioButton

関連コース: PythonPyQt5でGUIアプリを作成する

ラジオボタン

PyQTラジオボタンの例

以下のコードは、3つのラジオボタンを作成します。 グリッドに3つのラジオボタンを追加します。
ラジオボタンのいずれかをクリックすると、メソッドが呼び出されます onClicked()。 ラジオボタンは、を使用してそのメソッドに接続されます radiobutton.toggled.connect(self.onClicked)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)

radiobutton = QRadioButton("Australia")
radiobutton.setChecked(True)
radiobutton.country = "Australia"
radiobutton.toggled.connect(self.onClicked)
layout.addWidget(radiobutton, 0, 0)

radiobutton = QRadioButton("China")
radiobutton.country = "China"
radiobutton.toggled.connect(self.onClicked)
layout.addWidget(radiobutton, 0, 1)

radiobutton = QRadioButton("Japan")
radiobutton.country = "Japan"
radiobutton.toggled.connect(self.onClicked)
layout.addWidget(radiobutton, 0, 2)

def onClicked(self):
radioButton = self.sender()
if radioButton.isChecked():
print("Country is %s" % (radioButton.country))

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())

Python PyQtを初めて使用する場合は、 それなら私はこの本を強くお勧めします。

例をダウンロード

Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です