Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9aafd4

Browse files
authored
Merge pull request #42 from pythonguis/examples-from-articles
Examples from articles
2 parents b74592e + 33c6956 commit f9aafd4

File tree

104 files changed

+3040
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+3040
-0
lines changed

‎pyqt5/tutorials/basic-widgets/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# PyQt5 Widgets
2+
3+
This folder contains the code examples for the PythonGUIs tutorial [PyQt5 Widgets](https://www.pythonguis.com/tutorials/pyqt-basic-widgets/).
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import (
4+
QApplication,
5+
QCheckBox,
6+
QComboBox,
7+
QDateEdit,
8+
QDateTimeEdit,
9+
QDial,
10+
QDoubleSpinBox,
11+
QFontComboBox,
12+
QLabel,
13+
QLCDNumber,
14+
QLineEdit,
15+
QMainWindow,
16+
QProgressBar,
17+
QPushButton,
18+
QRadioButton,
19+
QSlider,
20+
QSpinBox,
21+
QTimeEdit,
22+
QVBoxLayout,
23+
QWidget,
24+
)
25+
26+
27+
class MainWindow(QMainWindow):
28+
def __init__(self):
29+
super().__init__()
30+
self.setWindowTitle("Widgets App")
31+
32+
layout = QVBoxLayout()
33+
widgets = [
34+
QCheckBox,
35+
QComboBox,
36+
QDateEdit,
37+
QDateTimeEdit,
38+
QDial,
39+
QDoubleSpinBox,
40+
QFontComboBox,
41+
QLCDNumber,
42+
QLabel,
43+
QLineEdit,
44+
QProgressBar,
45+
QPushButton,
46+
QRadioButton,
47+
QSlider,
48+
QSpinBox,
49+
QTimeEdit,
50+
]
51+
52+
for w in widgets:
53+
layout.addWidget(w())
54+
55+
widget = QWidget()
56+
widget.setLayout(layout)
57+
self.setCentralWidget(widget)
58+
59+
60+
app = QApplication(sys.argv)
61+
window = MainWindow()
62+
window.show()
63+
app.exec()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
3+
from PyQt5.QtCore import Qt
4+
from PyQt5.QtWidgets import (
5+
QApplication,
6+
QCheckBox,
7+
QComboBox,
8+
QDial,
9+
QDoubleSpinBox,
10+
QLabel,
11+
QLineEdit,
12+
QListWidget,
13+
QMainWindow,
14+
QSlider,
15+
QSpinBox,
16+
)
17+
18+
19+
class MainWindow(QMainWindow):
20+
21+
def __init__(self):
22+
super(MainWindow, self).__init__()
23+
self.setWindowTitle("My App")
24+
25+
26+
app = QApplication(sys.argv)
27+
w = MainWindow()
28+
w.show()
29+
app.exec()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
from PyQt5.QtCore import Qt
4+
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
5+
6+
7+
class MainWindow(QMainWindow):
8+
9+
def __init__(self):
10+
super(MainWindow, self).__init__()
11+
12+
self.setWindowTitle("My App")
13+
14+
widget = QLabel("Hello")
15+
font = widget.font()
16+
font.setPointSize(30)
17+
widget.setFont(font)
18+
widget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
19+
20+
self.setCentralWidget(widget)
21+
22+
23+
app = QApplication(sys.argv)
24+
w = MainWindow()
25+
w.show()
26+
app.exec()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
from PyQt5.QtCore import Qt
4+
from PyQt5.QtGui import QPixmap
5+
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
6+
7+
8+
class MainWindow(QMainWindow):
9+
10+
def __init__(self):
11+
super(MainWindow, self).__init__()
12+
13+
self.setWindowTitle("My App")
14+
15+
widget = QLabel()
16+
widget.setPixmap(QPixmap("otje.jpg"))
17+
widget.setScaledContents(True)
18+
widget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
19+
20+
self.setCentralWidget(widget)
21+
22+
23+
app = QApplication(sys.argv)
24+
w = MainWindow()
25+
w.show()
26+
app.exec()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
3+
from PyQt5.QtCore import Qt
4+
from PyQt5.QtWidgets import QApplication, QCheckBox, QMainWindow
5+
6+
7+
class MainWindow(QMainWindow):
8+
def __init__(self):
9+
super(MainWindow, self).__init__()
10+
11+
self.setWindowTitle("My App")
12+
13+
widget = QCheckBox()
14+
widget.setCheckState(Qt.Checked)
15+
16+
widget.stateChanged.connect(self.show_state)
17+
18+
self.setCentralWidget(widget)
19+
20+
def show_state(self, s):
21+
print(s == Qt.Checked)
22+
print(s)
23+
24+
25+
app = QApplication(sys.argv)
26+
w = MainWindow()
27+
w.show()
28+
app.exec()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QComboBox, QMainWindow
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super(MainWindow, self).__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
widget = QComboBox()
13+
widget.addItems(["One", "Two", "Three"])
14+
15+
widget.currentIndexChanged.connect(self.index_changed)
16+
17+
widget.currentTextChanged.connect(self.text_changed)
18+
19+
self.setCentralWidget(widget)
20+
21+
def index_changed(self, i):
22+
print(i)
23+
24+
def text_changed(self, s):
25+
print(s)
26+
27+
28+
app = QApplication(sys.argv)
29+
w = MainWindow()
30+
w.show()
31+
app.exec()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QListWidget, QMainWindow
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super(MainWindow, self).__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
widget = QListWidget()
13+
widget.addItems(["One", "Two", "Three"])
14+
15+
widget.currentItemChanged.connect(self.index_changed)
16+
widget.currentTextChanged.connect(self.text_changed)
17+
18+
self.setCentralWidget(widget)
19+
20+
def index_changed(self, i):
21+
print(i.text())
22+
23+
def text_changed(self, s):
24+
print(s)
25+
26+
27+
app = QApplication(sys.argv)
28+
w = MainWindow()
29+
w.show()
30+
app.exec()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QLineEdit, QMainWindow
4+
5+
6+
class MainWindow(QMainWindow):
7+
8+
def __init__(self):
9+
super(MainWindow, self).__init__()
10+
11+
self.setWindowTitle("My App")
12+
13+
widget = QLineEdit()
14+
widget.setMaxLength(10)
15+
widget.setPlaceholderText("Enter your text")
16+
17+
# widget.setReadOnly(True) # uncomment this to make readonly
18+
19+
widget.returnPressed.connect(self.return_pressed)
20+
widget.selectionChanged.connect(self.selection_changed)
21+
widget.textChanged.connect(self.text_changed)
22+
widget.textEdited.connect(self.text_edited)
23+
24+
self.setCentralWidget(widget)
25+
26+
def return_pressed(self):
27+
print("Return pressed!")
28+
self.centralWidget().setText("BOOM!")
29+
30+
def selection_changed(self):
31+
print("Selection changed")
32+
print(self.centralWidget().selectedText())
33+
34+
def text_changed(self, s):
35+
print("Text changed...")
36+
print(s)
37+
38+
def text_edited(self, s):
39+
print("Text edited...")
40+
print(s)
41+
42+
43+
app = QApplication(sys.argv)
44+
w = MainWindow()
45+
w.show()
46+
app.exec()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QMainWindow, QSpinBox
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
widget = QSpinBox()
13+
# Or: widget = QDoubleSpinBox()
14+
15+
widget.setMinimum(-9)
16+
widget.setMaximum(3)
17+
# Or: widget.setRange(-9, 3)
18+
19+
widget.setPrefix("$")
20+
widget.setSuffix("c")
21+
widget.setSingleStep(3) # Or e.g. 3.0 for QDoubleSpinBox
22+
widget.valueChanged.connect(self.value_changed)
23+
widget.textChanged.connect(self.value_changed_str)
24+
25+
self.setCentralWidget(widget)
26+
27+
def value_changed(self, i):
28+
print(i)
29+
30+
def value_changed_str(self, s):
31+
print(s)
32+
33+
34+
app = QApplication(sys.argv)
35+
w = MainWindow()
36+
w.show()
37+
app.exec()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /