Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PyQt5中居中对齐QLabel?_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python 如何在PyQt5中居中对齐QLabel?

Python 如何在PyQt5中居中对齐QLabel?,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,这是一个刽子手游戏项目,我正在Windows 7机器上使用PyQt5和Python进行工作。我已经使用相同的设置创建了两个小项目(一个抛硬币模拟器和一个掷骰子模拟器)。我还是一个初学者,这是我的第三个也是最近的一个“主要”项目。我无法获得空白的字,以对齐到中心的主窗口,似乎无法找出它。欢迎提供任何帮助/建议。:) 我认为您遇到的问题是,您没有为标签指定明确的宽度,因此它只使用它需要的任何空间。我会将标签宽度设置为主窗口小部件宽度,然后仅垂直移动,即: #Created using PyQt5 a

这是一个刽子手游戏项目,我正在Windows 7机器上使用PyQt5和Python进行工作。我已经使用相同的设置创建了两个小项目(一个抛硬币模拟器和一个掷骰子模拟器)。我还是一个初学者,这是我的第三个也是最近的一个“主要”项目。我无法获得空白的字,以对齐到中心的主窗口,似乎无法找出它。欢迎提供任何帮助/建议。:)

我认为您遇到的问题是,您没有为标签指定明确的宽度,因此它只使用它需要的任何空间。我会将标签宽度设置为主窗口小部件宽度,然后仅垂直移动,即:

#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_())
或者,我认为可以让标签自动调整大小,然后根据其宽度和父宽度调整其左侧。在任何情况下,要使其居中,您应该始终确保
label.width()+2*label.left()==form.width()
。即使label.left()为负/label.width()出于某种原因高于form.width(),这也应该可以工作。

您指的是哪个QLabel,你也可以把文本的大小放在它的几何图形的中心。@ EyLLANESC我想把用户猜测的单词的空白版本对齐到窗口的中心,因为所有的单词都有不同的长度。你应该改变字体大小,尝试你提出的解决方案,如果失败了,让我知道另找一个。P@eyllanesc非常感谢。费尔南多的建议奏效了。它还没有完成,所以我可能会改变一些东西。非常感谢。这似乎解决了问题。很抱歉,我不能提高投票率,因为我没有足够的声誉,但这对我很有效。再次感谢。:)
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)