iT邦幫忙

2021 iThome 鐵人賽

DAY 25
0
Software Development

Python GUI 專案設計模式及好用的開發技巧系列 第 26

自己做個好用的pysdie 2 cheat sheet

  • 分享至 

  • xImage
  •  

在很多地方都流行可以隨時查找的cheatsheet,那PySide2 有嗎? 筆者不清楚,乾脆直接做一個好了 ....

INSTALL

  • pip3 install PySide2

Hello World

from PySide2.QtWidgets import *
app = QApplication([]) 
window = QWidget() 
layout = QVBoxLayout() 
button = QPushButton("Push Button") 
layout.addWidget(QLabel('Hello World!')) 
layout.addWidget(button) 
window.setLayout(layout) 
window.show() 
app.exec_() 

Widgets

from PySide2.QtCore import Qt
from PySide2.QtWidgets import *
app = QApplication([])
window = QWidget()
layout = QVBoxLayout() 
slipper = QSlider(Qt.Horizontal) 
button = QPushButton(“Push button")
layout.addWidget(QLabel('Hello World!'))
layout.addWidget(button)
layout.addWidget(slipper)
window.setLayout(layout)
window.show()
app.exec_()

Layout

from PySide2.QtWidgets import *
app = QApplication([])
window = QWidget()
layout = QHBoxLayout()
label = QLabel("O_o ....o_O")
button1 = QPushButton("One")
button2 = QPushButton("Two")
button3 = QPushButton("Three")
layout.addWidget(label)
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(button3)
window.setLayout(layout)
window.show()
app.exec_()

chart

from PySide2 import QtGui
from PySide2.QtWidgets import *
from PySide2.QtCharts import QtCharts
from PySide2.QtGui import QPainter
from random import randint 
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
chart = QtCharts.QChart()
lineSeries = QtCharts.QLineSeries()

dt = [(i+1, randint(0, 99999)) for i in range(300)]
for point in dt:
    ls.append(point[0],point[1])
chart.addSeries(ls)
chart.setTitle("RNS")
chart.createDefaultAxes()
cv = QtCharts.QChartView(chart)
cv.setRenderHint(QPainter.Antialiasing)
ls.setColor(QtGui.QColor("yellow"))
cv.chart().setBackgroundBrush(QtGui.QColor("green"))
layout.addWidget(cv)
window.setLayout(layout)
window.show()
window.resize(700, 500)
app.exec_()

上一篇
PySide2和PyQt5區別
下一篇
Python GUI 好用的設計觀念
系列文
Python GUI 專案設計模式及好用的開發技巧31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言