화면에 문자열을 표시하기 위한 qt 시도

8949 단어 Qt
화면에 문자열을 표시하려면:

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"

좋은 웹페이지 즐겨찾기