#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。我已经使用相同的设置创建了两个小项目(硬币翻转模拟器和模具辊模拟器)。我还是初学者,这是我的第三个也是最近的#34;主要"项目。我无法得到单词的空白版本与主窗口的中心对齐,似乎无法弄明白。欢迎任何帮助/建议。 :)
答案 0 :(得分:1)
我认为你遇到的问题是你没有给你的标签明确宽度,因此它只是使用它需要的任何空间。我将标签宽度设置为主要的小部件宽度,然后仅垂直移动,即:
self.blank_word_label = QLabel(blank_word, self)
self.blank_word_label.setFixedWidth(162)
# Or, if you reorder your code so that your self.setGeometry(1427, ...) line occurs before this code, you could do directly:
# self.blank_word_label.setFixedWidth(self.width())
self.blank_word_label.move(0, 200)
self.blank_word_label.setAlignment(Qt.AlignCenter)
或者,我认为你可以让标签自动调整,然后根据宽度和父宽度调整左边。无论如何,要使它居中,你应该始终确保
label.width() + 2 * label.left() == form.width()
。即使label.left()为负数/ label.width()由于某种原因高于form.width(),这也应该有效。