-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest11.py
55 lines (44 loc) · 1.7 KB
/
test11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout, QFileDialog
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建按钮
btn = QPushButton('打开图片文件', self)
btn.clicked.connect(self.showDialog)
# 创建标签用于显示图片
self.image_label = QLabel(self)
self.image_label.setAlignment(Qt.AlignCenter)
# 设置窗口布局
vbox = QVBoxLayout()
vbox.addWidget(btn)
vbox.addWidget(self.image_label)
self.setLayout(vbox)
# 设置窗口的基本属性
self.setGeometry(300, 300, 400, 300)
self.setWindowTitle('PyQt5 打开图片文件示例')
self.show()
def showDialog(self):
# 打开文件对话框,选择图片文件
fname = QFileDialog.getOpenFileName(self, '打开图片文件', '/home', 'Image files (*.jpg *.png *.bmp)')
# 获取文件名
filename = fname[0]
#return filename
self.showImage(filename)
def showImage(self, filename):
# 显示选择的图片
pixmap = QPixmap(filename)
self.image_label.setPixmap(
pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
# 将文件名传递给后面的函数,这里只是打印文件名
print(filename)
return filename
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
#print(window.showDialog())
sys.exit(app.exec_())