#Created using PyQt5 and Python 3.5 on Windows 7
from sys import (exit, argv)
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QToolTip, QPushButton, QApplication, QWidget, QLabel)
from PyQt5.QtGui import (QIcon, QPixmap, QFont)
from random import choice
#Word list for the words the user will attempt to guess
words = ['Captivity', 'America', 'Europe', 'Federal', 'Gluten', 'Ridiculous', 'Automatic', 'Television', 'Difficult', 'Severe', 'Interesting', 'Indonesia', 'Industrial',
'Automotive', 'President', 'Terrestrial', 'Academic', 'Comedic', 'Comical', 'Genuine', 'Suitcase', 'Vietnam', 'Achievement', 'Careless', 'Monarchy', 'Monetary',
'Quarantine', 'Supernatural', 'Illuminate', 'Optimal', 'Application', 'Scientist', 'Software', 'Hardware', 'Program', 'Colonial', 'Algorithm', 'Intelligent']
#Creates the main widget which will contain everything else
class hangman(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
#Creates the QLabel 'background' which will contain the white background
self.background = QLabel(self)
#Uses QPixmap to place the background into the QLabel 'background'
self.background.setPixmap(QPixmap('background.jpg').scaled(162, 352, Qt.IgnoreAspectRatio, Qt.FastTransformation))
self.background.move(0.5, 0.5)
#Creates the QLabel 'image' which will contain the image of the hangman
self.image = QLabel(self)
#Uses QPixmap to insert the image of the hangman into the QLabel 'image'
self.image.setPixmap(QPixmap('hangman_7.png').scaled(78, 200, Qt.KeepAspectRatio, Qt.FastTransformation))
self.image.move(39, 0.5)
#Chooses random word from list 'words'
word = choice(words)
#Creates a blank version of the chosen word
blank_word = ''
for i in word:
blank_word += '__ '
self.blank_word_label = QLabel(blank_word, self)
self.blank_word_label.move(20,200)
#This doesn't work!!!
self.blank_word_label.setAlignment(Qt.AlignCenter)
#Sets where on the screen the window will open and the size of the window respectively using x and y coordinates
self.setGeometry(1427, 30, 162, 231)
#Locks the size of the window and make it impossible for the user to change it
self.setFixedSize(self.size())
self.setWindowTitle('Hangman')
#Sets the window icon to the image file 'icon.png' located in the same folder as the source file
self.setWindowIcon(QIcon('icon.png'))
self.show()
if __name__ == '__main__':
#Begins the execution of the QApplication
app = QApplication(argv)
ex = hangman()
ex.show()
exit(app.exec_())
這是一個hang子手遊戲項目,我正在使用Windows 7機器上的PyQt5和Python。我已經使用相同的設置創建了兩個小項目(一個硬幣翻轉模擬器和一個模具卷模擬器)。我仍然是初學者,這是我第三次和最近的「重大」項目。我無法將單詞的空白版本與主窗口的中心對齊,以至於無法弄清楚。歡迎任何幫助/建議。 :)如何在PyQt5中對齊QLabel?
對於您指的是哪種QLabel,如果文本的幾何尺寸較小,也可以將文本居中。 – eyllanesc
http://imgur.com/a/auPWF –
@eyllanesc我想將用戶猜測的單詞的空白版本與窗口中心對齊,因爲所有單詞都有不同的長度。 –