对话框
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QInputDialog, QPushButton
class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
self.btn = QPushButton('Dialog', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog)
self.le = QLineEdit(self)
self.le.move(130, 20)
self.move(300, 300)
self.resize(300, 200)
self.setWindowTitle('dialog')
self.show()
def showDialog(self):
text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
if ok:
self.le.setText(str(text.encode('utf-8')))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())
颜色选择器
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFrame, QColorDialog
from PyQt5.QtGui import QColor
class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
col = QColor(100, 100, 123)
self.btn = QPushButton('dialog', self)
self.btn.move(100, 100)
self.btn.clicked.connect(self.showDialog)
self.frm = QFrame(self)
self.frm.setStyleSheet("QWidget {background-color: %s}" % col.name())
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('colordialog')
self.show()
def showDialog(self):
col = QColorDialog.getColor()
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s}" % col.name())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())
字体选择器
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QSizePolicy, QLabel, QFontDialog
class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
vbox = QVBoxLayout()
btn = QPushButton('Dialog', self)
btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
btn.move(20, 20)
vbox.addWidget(btn)
btn.clicked.connect(self.showDialog)
self.lbl = QLabel('Font 字体', self)
self.lbl.move(130, 20)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('fontdialog')
self.show()
def showDialog(self):
font, ok = QFontDialog.getFont()
if ok:
self.lbl.setFont(font)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())
文件选择器
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QTextEdit, QAction
from PyQt5.QtGui import QIcon
class Myform(QMainWindow):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
openFile = QAction(QIcon('1.jpg'), 'Open', self)
openFile.setStatusTip('Open new File')
openFile.setShortcut('Ctrl+O')
openFile.triggered.connect(self.showDialog)
toolbar = self.addToolBar('Openfile')
toolbar.addAction(openFile)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('filedialog')
self.show()
def showDialog(self):
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home/linux')
if fname[0]:
with open(fname[0], 'r') as f:
data = f.read()
self.textEdit.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())