上傳檔案到「pyqt5/CODE/元件使用範例」

This commit is contained in:
M11212051 2024-05-05 22:29:59 +08:00
parent f7d8ad4dd9
commit 68543f9046
5 changed files with 225 additions and 0 deletions

View File

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QDockWidget範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class DockDemo(QMainWindow):
def __init__(self, parent=None):
super(DockDemo, self).__init__(parent)
layout = QHBoxLayout()
bar=self.menuBar()
file=bar.addMenu("&File")
file.addAction("&New")
file.addAction("&Save")
file.addAction("&Quit")
self.items = QDockWidget("Dockable", self)
self.listWidget = QListWidget()
self.listWidget.addItem("item1")
self.listWidget.addItem("item2")
self.listWidget.addItem("item3")
self.items.setWidget(self.listWidget)
self.items.setFloating(False)
self.setCentralWidget(QTextEdit())
self.addDockWidget(Qt.RightDockWidgetArea, self.items)
self.setLayout(layout)
self.setWindowTitle("Dock範例")
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = DockDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QFileDialog 範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class filedialogdemo(QWidget):
def __init__(self, parent=None):
super(filedialogdemo, self).__init__(parent)
layout = QVBoxLayout()
self.btn = QPushButton("載入圖片")
self.btn.clicked.connect(self.getfile)
layout.addWidget(self.btn)
self.le = QLabel("")
layout.addWidget(self.le)
self.btn1 = QPushButton("載入文字檔")
self.btn1.clicked.connect(self.getfiles)
layout.addWidget(self.btn1)
self.contents = QTextEdit()
layout.addWidget(self.contents)
self.setLayout(layout)
self.setWindowTitle("File Dialog 範例")
def getfile(self):
fname, _ = QFileDialog.getOpenFileName(self, 'Open File', 'c:\\',"Image Files (*.jpg *.gif)")
self.le.setPixmap(QPixmap(fname))
def getfiles(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
dlg.setFilter( QDir.Files )
if dlg.exec_():
filenames= dlg.selectedFiles()
f = open(filenames[0], 'r')
with f:
data = f.read()
self.contents.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = filedialogdemo()
ex.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QFontDialog範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class FontDialogDemo(QWidget):
def __init__(self, parent=None):
super(FontDialogDemo, self).__init__(parent)
layout = QVBoxLayout()
self.fontButton = QPushButton("choose font")
self.fontButton .clicked.connect(self.getFont)
layout.addWidget(self.fontButton )
self.fontLineEdit = QLabel("Hello, 測試字體範例")
layout.addWidget(self.fontLineEdit )
self.setLayout(layout)
self.setWindowTitle("Font Dialog範例")
def getFont(self):
font, ok = QFontDialog.getFont()
if ok:
self.fontLineEdit .setFont(font)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = FontDialogDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QInputDialog範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class InputdialogDemo(QWidget):
def __init__(self, parent=None):
super(InputdialogDemo, self).__init__(parent)
layout = QFormLayout()
self.btn1 = QPushButton("取得清單裡的選項")
self.btn1.clicked.connect(self.getItem)
self.le1 = QLineEdit()
layout.addRow(self.btn1,self.le1)
self.btn2 = QPushButton("取得字串")
self.btn2.clicked.connect(self.getIext)
self.le2 = QLineEdit()
layout.addRow(self.btn2,self.le2)
self.btn3 = QPushButton("取得整數")
self.btn3.clicked.connect(self.getInt)
self.le3 = QLineEdit()
layout.addRow(self.btn3,self.le3)
self.setLayout(layout)
self.setWindowTitle("Input Dialog範例")
def getItem(self):
items = ("C", "C++", "Java", "Python")
item, ok = QInputDialog.getItem(self, "Select Input Dialog",
"語言清單:", items, 0, False)
if ok and item:
self.le1.setText(item)
def getIext(self):
text, ok = QInputDialog.getText(self, 'Text Input Dialog', '輸入姓名:')
if ok:
self.le2.setText(str(text))
def getInt(self):
num,ok = QInputDialog.getInt(self,"Integer Input Dialog", "輸入數字:")
if ok:
self.le3.setText(str(num))
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = InputdialogDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QMessage範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class WinForm( QWidget):
def __init__(self):
super(WinForm,self).__init__()
self.setWindowTitle("QMessageBox範例")
self.resize(300, 100)
self.myButton = QPushButton(self)
self.myButton.setText("點擊彈出訊息對話方塊")
self.myButton.clicked.connect(self.msg)
def msg(self):
# 使用infomation訊息對話方塊
reply = QMessageBox.information(self, "標題", "訊息文字", QMessageBox.Yes | QMessageBox.No , QMessageBox.Yes )
# reply = QMessageBox.information(self, "標題", "訊息對話方塊文字", QMessageBox.Yes | QMessageBox.No , QMessageBox.Yes )
# reply = QMessageBox.question(self, "標題", "提問框訊息文字", QMessageBox.Yes | QMessageBox.No , QMessageBox.Yes )
# reply = QMessageBox.warning(self, "標題", "警告框訊息文字", QMessageBox.Yes | QMessageBox.No , QMessageBox.Yes )
# reply = QMessageBox.critical(self, "標題", "嚴重錯誤對話方塊訊息文字", QMessageBox.Yes | QMessageBox.No , QMessageBox.Yes )
# reply = QMessageBox.about(self, "標題", "關於對話方塊" )
print( reply )
if __name__ == '__main__':
app= QApplication(sys.argv)
demo = WinForm()
demo.show()
sys.exit(app.exec_())