#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_())
これは、私がWindows 7マシンでPyQt5とPythonを使用して作業しているハングマンゲームプロジェクトです。私は同じセットアップ(コインフリップシミュレータとダイロールシミュレータ)を使って2つの小さなプロジェクトを作成しました。私はまだ初心者ですが、これは私の3番目で最も最近の「主要な」プロジェクトです。私はそれを把握していないようにするために、メインウィンドウの中心に合わせるために空白のバージョンの単語を得ることができません。助けやアドバイスは大歓迎です。 :)QLabelをPyQt5でどのようにセンタリングすることができますか?
QLabelの意味は、テキストのジオメトリのサイズが小さい場合にテキストを中央に配置することもできます。 – eyllanesc
http://imgur.com/a/auPWF –
@eyllanescすべての単語の長さが異なるため、ユーザーが推測している単語の空白のバージョンをウィンドウの中央に揃えたいとします。 –