0
Help us understand the problem. What are the problem?

posted at

updated at

qtを使用して文字列を画面に表示するだけの試み

以下のような、文字列を画面に表示するだけの試み
test.png

1年に1回くらいこのような文字列を表示したい時がある。
以下はコードと実行方法

環境:ubuntu20.04

display.py
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui 
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys 
from argparse import ArgumentParser
import subprocess

def get_option(player_name):
    argparser = ArgumentParser()
    argparser.add_argument('--player_name', type=str,
                           default=player_name,
                           help='player name')
    return argparser.parse_args()

class Window(QMainWindow): 

    def __init__(self): 
        super().__init__() 

        # setting title 
        self.setWindowTitle("Player information") 

        # show information
        self.player_name = "testuser"
        args = get_option(self.player_name)
        if len(args.player_name) != 0:
            self.player_name = args.player_name

        # setting geometry
        upper_left = (10,10)
        width_height = (500, 75)
        self.setGeometry(upper_left[0], upper_left[1],
                         width_height[0], width_height[1]) 

        # calling method 
        self.UiComponents() 

        # showing all the widgets 
        self.show() 

    # method for widgets 
    def UiComponents(self): 

        # creating a label to show the time 
        self.label = QLabel(self)
        label_upper_left = (15, 15)
        label_width_height = (450, 50)
        self.label.setGeometry(label_upper_left[0], label_upper_left[1], 
                               label_width_height[0], label_width_height[1]) 
        self.label.setStyleSheet("border : 4px solid black;") 
        self.label.setText(self.gettimertext())
        self.label.setFont(QFont('Arial', 28))
        self.label.setAlignment(Qt.AlignCenter) 

    def gettimertext(self):
        text = "Player: " + self.player_name
        return text

# create pyqt5 app 
App = QApplication(sys.argv) 

# create the instance of our Window 
window = Window() 

# start the app 
sys.exit(App.exec())

以下のように実行する

python3 display.py --player_name "test_string"
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?