2017-08-03 61 views
-1
#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我想將用戶猜測的單詞的空白版本與窗口中心對齊,因爲所有單詞都有不同的長度。 –

回答

0

我認爲你遇到的問題是你沒有給你的標籤明確的寬度,因此它只是使用它需要的任何空間。我將設置標籤寬度的主窗口部件的寬度,然後將它只有垂直,即:

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

非常感謝。這似乎解決了這個問題。對不起,我無法贊成,因爲我沒有足夠的聲譽,但這對我有效。再次感謝。 :) –