如何在PyQt5中对齐QLabel?

问题描述:

#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?

+0

对于您指的是哪种QLabel,如果文本的几何尺寸较小,也可以将文本居中。 – eyllanesc

+0

http://imgur.com/a/auPWF –

+0

@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) 

另外,我想你可以让标签可以自动调整大小,然后调整基于其宽度和其左父宽度。无论如何,为了使其居中,您应该始终确保 label.width() + 2 * label.left() == form.width()。即使label.left()为负值,label.width()由于某种原因高于form.width(),这应该可以工作。

+1

非常感谢。这似乎解决了这个问题。对不起,我无法赞成,因为我没有足够的声誉,但这对我有效。再次感谢。 :) –