I am trying to learn PyQt5 and I was wondering if it is possible to have a condensed form of what I have shown below. The below Already works but i was hoping that maybe there is a condensed version. All the buttons will end up doing the same thing, which is filling in the word for the hangman game I am making.
Now i have tried a for loop that looks like this. where every thing else in the program is the same but now I inserted a for loop thinking maybe that would fix the length problem.
def initUI(self):
self.setGeometry(700, 250, 700, 700)
self.setWindowTitle("Hang-Man")
btn=[]
for i in range(27):
btn.append(i)
j=i*25
btn[i].move(20,int(j))
btn[i] = QPushButton(chr(i), self)
btn[i].clicked.connect(self.close)
Here is the code that actually works as intended though. This goes all the way to z with the increments shown here.
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.show()
def initUI(self):
self.setGeometry(700, 250, 700, 700)
self.setWindowTitle("Hang-Man")
btn1 = QPushButton('A', self)
btn1.move(20, 25)
btn1.clicked.connect(self.close)
btn2 = QPushButton('B', self)
btn2.move(20, 50)
btn2.clicked.connect(self.close)
btn3 = QPushButton('C', self)
btn3.move(20, 75)
btn3.clicked.connect(self.close)
... #this continues all the way to Z
def run():
app = QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())`
run()
Just use a loop: