您的位置:首页 > 编程语言 > Qt开发

PyQt5的学习之路(六)

2017-01-18 16:30 211 查看

对话框

#! /usr/bin/python
# -*- coding: utf-8 -*-

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):

# 返回值:文本内容和一个布尔值,参数: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_())


颜色选择器

#! /usr/bin/python
# -*- coding: utf-8 -*-

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)

# 初始化QFrame的颜色
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()    # ColorPicker

if col.isValid():   # 当点击Ok时col.isValid()为True,点击Cancel或关闭为False

self.frm.setStyleSheet("QWidget { background-color: %s}" % col.name())

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())


字体选择器

#! /usr/bin/python
# -*- coding: utf-8 -*-

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()    # FontPicker
if ok:
self.lbl.setFont(font)

if __name__ == '__main__':

app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())


文件选择器

#! /usr/bin/python
# -*- coding: utf-8 -*-

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')   #FilePicker

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_())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pyqt 对话框