1.主要用到 qfiledialog 方法打开本地文件
2.界面
打开前:
打开后:
3. 代码
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
|
import sys
from pyqt5 import qtwidgets, qtcore, qtgui
from pyqt5.qtgui import *
from pyqt5.qtwidgets import *
from pyqt5.qtcore import *
class picture(qwidget):
def __init__( self ):
super (picture, self ).__init__()
self .resize( 600 , 400 )
self .setwindowtitle( "label显示图片" )
self .label = qlabel( self )
self .label.settext( " 显示图片" )
self .label.setfixedsize( 300 , 200 )
self .label.move( 160 , 160 )
self .label.setstylesheet( "qlabel{background:white;}"
"qlabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}"
)
btn = qpushbutton( self )
btn.settext( "打开图片" )
btn.move( 10 , 30 )
btn.clicked.connect( self .openimage)
def openimage( self ):
imgname, imgtype = qfiledialog.getopenfilename( self , "打开图片" , " ", " * .jpg;; * .png;; all files( * )")
jpg = qtgui.qpixmap(imgname).scaled( self .label.width(), self .label.height())
self .label.setpixmap(jpg)
if __name__ = = "__main__" :
app = qtwidgets.qapplication(sys.argv)
my = picture()
my.show()
sys.exit(app.exec_())
|
以上这篇pyqt5 从本地选择图片 并显示在label上的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_32973061/article/details/81139689