Here is one simple code from Zetcode.com, that I am studying:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
In this example, we create a skeleton
of a calculator using a QGridLayout.
author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
QPushButton, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
names = ['Cls', 'Bck', '', 'Close',
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+']
positions = [(i,j) for i in range(5) for j in range(4)]
for position, name in zip(positions, names):
if name == '':
continue
button = QPushButton(name)
grid.addWidget(button, *position)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Now, I am trying to apply the button.setDisable
option for every button which is clicked.
One way is to create a new list and append each button to the created list. From this list, we could do following:
button_list[0].clicked.connect(self.on_click):
button_list[1].clicked.connect(self.on_click1):
And for each new method, we would then need to define:
def on.click(self):
button_list[0].setEnabled(False)
This is a solution that works. But is there any more dynamic way to solve this issue?
Would appreciate any ideas.
With lambda
or functools.partial
, you can do just that:
def on.click(self, numb):
button_list[numb].setEnabled(False)
Invoke with lambda
:
button_list[1].clicked.connect(lambda: self.on_click(1))
Or with partial
:
button_list[1].clicked.connect(partial(self.on_click, 1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.