2017-08-03 21 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_()) 

これは、私がWindows 7マシンでPyQt5とPythonを使用して作業しているハングマンゲームプロジェクトです。私は同じセットアップ(コインフリップシミュレータとダイロールシミュレータ)を使って2つの小さなプロジェクトを作成しました。私はまだ初心者ですが、これは私の3番目で最も最近の「主要な」プロジェクトです。私はそれを把握していないようにするために、メインウィンドウの中心に合わせるために空白のバージョンの単語を得ることができません。助けやアドバイスは大歓迎です。 :)QLabelをPyQt5でどのようにセンタリングすることができますか?

+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

ありがとうございました。これは問題を解決したようです。私は十分な評判を持っていないので、私はupvoteことができない申し訳ありませんが、これは私のために働いた。再度、感謝します。 :) –

関連する問題