-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple_buttons.py
74 lines (59 loc) · 2.46 KB
/
multiple_buttons.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# coding=utf-8
# imports the sys module
import sys # provides interaction with the Python interpreter
# imports the necessary Qt modules
from PyQt4 import QtGui # provides the graphic elements
from PyQt4.QtCore import pyqtSlot # provides the 'pyqtSlot()' decorator
# the 'Window' class inherits from the 'QWidget' class
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# button names
button_names = [
u'Button A',
u'Button B',
u'Button C',
u'Button D',
u'Button E',
u'Button F'
]
# creates the buttons
self.buttons = {}
for name in button_names:
button = QtGui.QPushButton(name)
button.clicked.connect(self.button_clicked) # connects the 'clicked()' signal with the 'button_clicked()' slot
self.buttons[name] = button
# creates a vertical box layout for the window
vlayout = QtGui.QVBoxLayout()
# adds each button to the layout
for button in button_names:
vlayout.addWidget(self.buttons[button])
self.setLayout(vlayout) # sets the window layout
# 'button_clicked()' slot
@pyqtSlot()
def button_clicked(self):
""" Prints the text of the button clicked """
# the 'sender()' method returns the object that emits the signal
if self.sender() is self.buttons[u'Button A']:
print u'button A clicked'
elif self.sender() is self.buttons[u'Button B']:
print u'button B clicked'
elif self.sender() is self.buttons[u'Button C']:
print u'button C clicked'
elif self.sender() is self.buttons[u'Button D']:
print u'button D clicked'
elif self.sender() is self.buttons[u'Button E']:
print u'button E clicked'
elif self.sender() is self.buttons[u'Button F']:
print u'button F clicked'
else:
print u'other button clicked'
# creates the application and takes arguments from the command line
application = QtGui.QApplication(sys.argv)
# creates the window and sets its properties
window = Window()
window.setWindowTitle(u'Multiple buttons') # title
window.resize(300, 200) # size
window.show() # shows the window
# runs the application and waits for its return value at the end
sys.exit(application.exec_())