alex_bn_lee

导航

[1023] PyQt: GUI for Python

ref: PyQt5 tutorial

ref: PyQt6 Widgets


Example:

Script:

from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget, QLineEdit, QPushButton, QVBoxLayout, QMessageBox
from PyQt6.QtGui import QIcon
import pyautogui

app = QApplication([])

window = QWidget()
# Window's title
window.setWindowTitle("TestApp")
# Window's icon
window.setWindowIcon(QIcon(r"D:\OneDrive\OneDrive - Land Insight Resources\Bingnan_Li\01_Tasks\2024\47_2024_07_08_Python_GUI\icon2.png"))
# Window's size
# window.resize(250, 80) 
# window.setFixedSize(250, 80) 
window.setFixedWidth(250) 

###################################################################################################

# For name 
label_name = QLabel("Name:")
line_edit_name = QLineEdit()

line_layout_name = QHBoxLayout()
line_layout_name.addWidget(label_name)
line_layout_name.addWidget(line_edit_name)

# For age  
label_age  = QLabel("Age:")
line_edit_age  = QLineEdit()

line_layout_age  = QHBoxLayout()
line_layout_age .addWidget(label_age )
line_layout_age .addWidget(line_edit_age )

# For city  
label_city  = QLabel("City:")
line_edit_city  = QLineEdit()

line_layout_city  = QHBoxLayout()
line_layout_city .addWidget(label_city )
line_layout_city .addWidget(line_edit_city )

layout_window = QVBoxLayout()

layout_window.addLayout(line_layout_name)
layout_window.addLayout(line_layout_age)
layout_window.addLayout(line_layout_city)

###################################################################################################

# For buttons
button_top = QPushButton('Get all above info')
button_bottom = QPushButton('Open Chrome')

layout_button = QHBoxLayout()
layout_button.addWidget(button_top)
layout_button.addWidget(button_bottom)

layout_window.addLayout(layout_button)

window.setLayout(layout_window)

def on_button_top_clicked():
    alert = QMessageBox()
    text_name = line_edit_name.text()
    text_age = line_edit_age.text()
    text_city = line_edit_city.text()
    alert.setText(f'My name is {text_name} and I am {text_age} years old now. \n Also, I am from {text_city}!')
    alert.exec()
    
button_top.clicked.connect(on_button_top_clicked)    

def on_button_bottom_clicked():
    pyautogui.hotkey("win", "2")
    #alert = QMessageBox()
    #alert.setText('You clicked the bottom button!')
    #alert.exec()
   
button_bottom.clicked.connect(on_button_bottom_clicked)    


window.show()
app.exec()

 

posted on 2024-07-08 14:57  McDelfino  阅读(2)  评论(0编辑  收藏  举报