ساخت برچسب یا QLabel

  • مدرس : علی بیگدلی
  • تاریخ انتشار: 1404/07/24
  • تعداد بازدید: 3

در این بخش می‌خواهیم متنی را در صفحه قرار دهیم تا بتوانیم نحوه‌ی نمایش آن را در طرح‌های مختلف بررسی کنیم. ساده‌ترین گام در شروع برنامه‌نویسی گرافیکی با PyQt6، نمایش یک نوشته در پنجره است. در PyQt6 برخلاف tkinter برای جایگذاری المان‌ها از Layoutها مانند QVBoxLayout، QHBoxLayout و ... استفاده می‌شود که در بخش‌های بعدی به‌صورت کامل بررسی خواهیم کرد. فعلاً هدف ما آشنایی کلی با نحوه‌ی ایجاد و نمایش برچسب (Label) است. با یک مثال ساده شروع می‌کنیم. برای ایجاد یک برچسب در صفحه از کلاس QLabel استفاده می‌کنیم. این برچسب در داخل یک پنجره از نوع QWidget یا QMainWindow نمایش داده می‌شود. در ادامه دو روش تابعی و شی‌گرایی را بررسی می‌کنیم:

# Function Example

# importing modules
import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout

# creating the application
app = QApplication(sys.argv)

# creating main window
window = QWidget()
window.setWindowTitle("icc-aria gui app")

# creating layout
layout = QVBoxLayout()

# adding a label to the layout
label = QLabel("Hello ARIA Students")
layout.addWidget(label)

# setting layout to the window
window.setLayout(layout)

# showing the window
window.show()

# executing the application loop
sys.exit(app.exec())
# OOP Example

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("icc-aria gui app")

        # layout setup
        layout = QVBoxLayout()

        # creating label
        label = QLabel("Hello ARIA Students")
        layout.addWidget(label)

        self.setLayout(layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec())

نمونه خروجی:

 

حال اگر بخواهید ویژگی‌های ظاهری برچسب مانند اندازه، فونت یا رنگ را تغییر دهید، می‌توانید از تنظیمات مربوط به فونت و استایل استفاده کنید. در PyQt6 این کار از طریق QFont و setStyleSheet انجام می‌شود. مثال زیر چند حالت مختلف را نشان می‌دهد:

# Function Example

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtGui import QFont

app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("thealibigdeli gui app")

layout = QVBoxLayout()

# default label with Tahoma font
label1 = QLabel("thealibigdeli example text")
label1.setFont(QFont("Tahoma"))
layout.addWidget(label1)

# label with font size 18
label2 = QLabel("thealibigdeli example text")
label2.setFont(QFont("Tahoma", 18))
layout.addWidget(label2)

# label with red text color
label3 = QLabel("thealibigdeli example text")
label3.setStyleSheet("color: red;")
layout.addWidget(label3)

# label with blue background and white text
label4 = QLabel("thealibigdeli example text")
label4.setStyleSheet("background-color: blue; color: white;")
layout.addWidget(label4)

window.setLayout(layout)
window.show()
sys.exit(app.exec())
# OOP Example

import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtGui import QFont

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("thealibigdeli gui app")

        layout = QVBoxLayout()

        label1 = QLabel("thealibigdeli example text")
        label1.setFont(QFont("Tahoma"))
        layout.addWidget(label1)

        label2 = QLabel("thealibigdeli example text")
        label2.setFont(QFont("Tahoma", 18))
        layout.addWidget(label2)

        label3 = QLabel("thealibigdeli example text")
        label3.setStyleSheet("color: red;")
        layout.addWidget(label3)

        label4 = QLabel("thealibigdeli example text")
        label4.setStyleSheet("background-color: blue; color: white;")
        layout.addWidget(label4)

        self.setLayout(layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec())

نمونه خروجی:

 

ثبت دیدگاه


نکته: آدرس ایمیل شما منتشر نخواهد شد

دیدگاه کاربران (0)


هیچ دیدگاهی ثبت نشده است. می‌توانید اولین نفر باشید.