一个图片按钮类

picButton.cs文件

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
using  System.Data;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  Streamsea.Properties;
None.gif
None.gif
namespace  Streamsea.Forms
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 图片按钮
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public partial class ucPictureButton : UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private bool bPressed;
InBlock.gif        
private Image pressImg;
InBlock.gif        
private Image backImg;
InBlock.gif        
private Image disableImg;
InBlock.gif
InBlock.gif        
private Image m_img;
InBlock.gif        
private PictureBoxSizeMode m_sizemode = PictureBoxSizeMode.StretchImage;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ucPictureButton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.Font = new Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private Size _GetPreferredSize()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.m_img == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.Size;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return this.m_img.Size;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private Rectangle _ImageRectangle
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Rectangle rectangle1 
= new Rectangle(0000);
InBlock.gif                
if (this.m_img != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
switch (this.m_sizemode)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case PictureBoxSizeMode.Normal:
InBlock.gif                            rectangle1.Size 
= this.m_img.Size;
InBlock.gif                            
return rectangle1;
InBlock.gif
InBlock.gif                        
case PictureBoxSizeMode.StretchImage:
InBlock.gif                            rectangle1.Size 
= base.ClientSize;
InBlock.gif                            
return rectangle1;
InBlock.gif
InBlock.gif                        
case ((PictureBoxSizeMode)2):
InBlock.gif                            
return rectangle1;
InBlock.gif
InBlock.gif                        
case PictureBoxSizeMode.CenterImage:
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                rectangle1.Size 
= this.m_img.Size;
InBlock.gif                                Size size1 
= base.ClientSize;
InBlock.gif                                rectangle1.X 
= (size1.Width - rectangle1.Width) / 2;
InBlock.gif                                rectangle1.Y 
= (size1.Height - rectangle1.Height) / 2;
InBlock.gif                                
return rectangle1;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return rectangle1;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 指示如何显示图像。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [DefaultValue(PictureBoxSizeMode.StretchImage)]
InBlock.gif        
public PictureBoxSizeMode SizeMode
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_sizemode;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!Enum.IsDefined(typeof(PictureBoxSizeMode), value))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new ArgumentException();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (this.m_sizemode != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.m_sizemode = value;
InBlock.gif                    
base.Invalidate();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 背景图像
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [DefaultValue((string)null)]
InBlock.gif        
public Image BackImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn backImg; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                backImg 
= value;
InBlock.gif                m_img 
= backImg;
InBlock.gif                
base.Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按下时的图像
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [DefaultValue((string)null)]
InBlock.gif        
public Image PressImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn pressImg; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pressImg 
= value;
InBlock.gif                
base.Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 不可用时的图像
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [DefaultValue((string)null)]
InBlock.gif        
public Image DisableImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn disableImg; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                disableImg 
= value;
InBlock.gif                m_img 
= disableImg;
InBlock.gif                
base.Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按钮文字
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string ButtonText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.Text = value;
InBlock.gif                
this.Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public new bool Enabled
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn base.Enabled; }
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.Enabled = value;
InBlock.gif                
this.Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 引发 System.Windows.Forms.Control.MouseDown 事件。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        protected override void OnMouseDown(MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.bPressed = true;
InBlock.gif            
this.Invalidate();
InBlock.gif            
base.OnMouseDown(e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 引发 System.Windows.Forms.Control.MouseUp 事件。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        protected override void OnMouseUp(MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.bPressed = false;
InBlock.gif            
this.Invalidate();
InBlock.gif            
base.OnMouseUp(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 绘制控件的背景。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        protected override void OnPaintBackground(PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Region region1 
= new Region(base.ClientRectangle);
InBlock.gif            region1.Exclude(
this._ImageRectangle);
InBlock.gif            e.Graphics.FillRegion(
new SolidBrush(this.Parent!=null?this.Parent.BackColor:Color.White), region1);
InBlock.gif            region1.Dispose();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 引发 System.Windows.Forms.Control.Paint 事件。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="e"></param>

InBlock.gif        protected override void OnPaint(PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.bPressed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (pressImg == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    pressImg 
= Resources.PressImage;
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_img 
= pressImg;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (this.Enabled)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (backImg == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    backImg 
= Resources.BackImage;
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_img 
= backImg;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (disableImg == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    disableImg 
= Resources.DisableImage;
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_img 
= disableImg;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            e.Graphics.DrawImage(
this.m_img, this._ImageRectangle, new Rectangle(00this.m_img.Width, this.m_img.Height), GraphicsUnit.Pixel);
InBlock.gif
InBlock.gif            
if (this.ButtonText.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SizeF size 
= e.Graphics.MeasureString(this.ButtonText, this.Font);
InBlock.gif                e.Graphics.DrawString(
this.ButtonText, this.Font, new SolidBrush(Enabled?this.ForeColor:Color.Gray),
InBlock.gif                    (
this.ClientSize.Width - size.Width) / 2, (this.ClientSize.Height - size.Height) / 2);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//e.Graphics.DrawRectangle(new Pen(this.Parent != null ? this.Parent.BackColor : Color.White), 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
InBlock.gif
            base.OnPaint(e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 引发 System.Windows.Forms.Control.Resize 事件。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="eventg"></param>

InBlock.gif        protected override void OnResize(EventArgs eventg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnResize(eventg);
InBlock.gif            
if ((this.m_sizemode == PictureBoxSizeMode.StretchImage) || (this.m_sizemode == PictureBoxSizeMode.CenterImage))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 picButton.Designer.cs

None.gif namespace  Streamsea.Forms
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    partial 
class ucPictureButton
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.IContainer components = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 清理所有正在使用的资源。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

InBlock.gif        protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (disposing && (components != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                components.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 设计器支持所需的方法 - 不要
InBlock.gif        
/// 使用代码编辑器修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

在项目属性的资源文件里添加3张图片,名字分别为 BackImage  DisableImage  PressImage,这是默认的图片,在设计和谐程序的时候也可以换用其他图片的。

转载于:https://www.cnblogs.com/zgvslch/archive/2006/10/10/525322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python和PyQt可以实现一个按钮打开一个文件夹中所有图片并显示,步骤如下: 1.导入必要的模块,包括os(用于文件夹及文件操作)、PyQt中的QtWidgets组件、PIL(Python Imaging Library,用于图片打开及显示)。 2.创建一个,继承自QtWidgets中的QMainWindow,用于创建显示窗口并添加按钮。 3.在中定义一个槽函数,用于处理按钮点击事件。槽函数中使用QFileDialog打开文件夹,使用os模块获取文件夹中所有图片文件的路径,并使用PIL打开并显示。 4.实例化并运行。 具体代码如下: ```python import os from PyQt5.QtWidgets import QMainWindow, QFileDialog, QApplication, QLabel, QWidget, QVBoxLayout from PyQt5.QtGui import QPixmap from PIL import Image class MainWindow(QMainWindow): def __init__(self): super().__init__() # 创建一个QWidget,用于添加显示图片的标签 im_widget = QWidget() self.setCentralWidget(im_widget) layout = QVBoxLayout() im_widget.setLayout(layout) # 创建一个按钮,用于打开文件夹 self.button = QtWidgets.QPushButton("打开文件夹") self.button.clicked.connect(self.open_folder) layout.addWidget(self.button) # 创建一个标签,用于显示图片 self.label = QLabel() layout.addWidget(self.label) def open_folder(self): # 打开文件夹对话框 folder = QFileDialog.getExistingDirectory(self, "选择文件夹") if folder: # 遍历文件夹,获取所有图片文件路径 for filename in os.listdir(folder): if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif')): filepath = os.path.join(folder, filename) # 使用PIL打开图片 image = Image.open(filepath) # 将PIL格式的图片转换为QPixmap pixmap = QPixmap.fromImage(ImageQt(image)) # 显示图片 self.label.setPixmap(pixmap) if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_() ``` 以上代码实现了一个窗口,并添加一个“打开文件夹”的按钮,点击该按钮,可以打开文件夹选择对话框,选择一个包含图片的文件夹后,该程序将遍历该文件夹,打开所有图片,并将其显示在窗口中。 ### 回答2: Python是一种高级编程语言,广泛用于开发各种应用程序。PyQT是一种基于Python的GUI(图形用户界面)编程工具包,可以方便地创建各种桌面应用程序。本文将介绍使用python pyqt实现一个按钮打开一个文件夹中所有图片并显示的过程。 第一步:安装 PyQT 在实现该功能之前,需要安装 PyQT 以及其他一些需要的库。可以使用pip命令安装它们。在终端或命令提示符中输入以下命令进行安装。 pip install PyQt5 pip install Pillow 完成安装后,就可以开始创建应用程序了。 第二步:创建GUI窗口 在pyqt中,可以使用QtDesigner应用程序创建GUI窗口。该程序可以使用拖放界面并设置组件属性的方式来创建GUI窗口。完成后,既可以将窗口转换为python代码,以方便在python应用程序中创建该窗口。创建完成后,可以使用以下代码将其加载到python应用程序中。 ```python from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.uic import loadUiType import os from PIL import Image Ui_MainWindow, QMainWindow = loadUiType('mainwindow.ui') ``` 请注意,代码的第一行引用了PyQT5中的必要模块。 第二行引用系统和Python模块。 通常,这是GUI的主要。 第三行使用loadUiType来加载GUI窗口的UI文件,然后使用此UI文件的创建主窗口的实例。 第三步:实现按钮操作 在实现该功能之前,需要在GUI窗口中添加按钮。添加后,可以使用以下代码实现按钮的操作。 ```python class Main(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(Main, self).__init__(parent) self.setupUi(self) self.pushButton.clicked.connect(self.show_images) def show_images(self): folder_path = QFileDialog.getExistingDirectory(self, "选择文件夹", os.getcwd()) if folder_path: for root, dirs, files in os.walk(folder_path): for file in files: if file.endswith(('jpg', 'png', 'jpeg', 'bmp')): file_path = os.path.join(root, file) image = Image.open(file_path) image.show() ``` 在上面的代码中,创建了一个名为Main的并继承了ui窗口。 show_images函数是按钮单击的槽功能。QFileDialog.getExistingDirectory方法用于打开一个文件夹,然后将显示文件夹中所有图片的窗口。然后,使用os.walk遍历文件夹中的文件,并查找文件是否是图像。如果是图像,则使用Pillow库中的Image打开它,并执行Image.show以在窗口中显示。完成后,该功能就可以使用了。 总结 在本文中,我们了解了Python pyqt如何实现一个按钮打开一个文件夹中所有图片并显示的功能。对于需要显示图像文件夹中的所有图像的应用程序来说,这是一种非常实用的功能。 该应用程序可以帮助用户更快地找到图像文件夹中的所有图像,并可以直接在应用程序中查看它们。希望这篇文章对Python和PyQt的学习有所帮助。 ### 回答3: 使用 Python 和 PyQt 实现一个按钮打开一个文件夹中所有图片并显示,需要经过以下几个步骤: 1. 导入必要的库和模块,包括 PyQt、os、Pillow(Python Imaging Library)等。 2. 创建一个界面,包括一个按钮一个用于显示图片的控件。 3. 给按钮添加事件处理方法,当按钮被点击时,弹出文件选择框,让用户选择需要打开的文件夹。 4. 遍历所选文件夹中的所有图片文件,使用 Pillow 库读取图片并显示在控件中。 下面是具体实现的代码: ```python import os from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QFileDialog from PyQt5.QtGui import QPixmap class MyWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): btn = QPushButton('打开文件夹', self) btn.resize(btn.sizeHint()) btn.move(20, 20) btn.clicked.connect(self.openFolder) self.label = QLabel(self) self.label.setGeometry(20, 60, 360, 360) self.setGeometry(300, 300, 400, 450) self.setWindowTitle('显示图片') self.show() def openFolder(self): folder_path = QFileDialog.getExistingDirectory(self, '选择文件夹', '/') if folder_path: self.showImages(folder_path) def showImages(self, folder_path): for filename in os.listdir(folder_path): if filename.endswith('.jpg') or filename.endswith('.png'): img_path = os.path.join(folder_path, filename) img = QPixmap(img_path) self.label.setPixmap(img) self.label.setScaledContents(True) if __name__ == '__main__': app = QApplication([]) window = MyWindow() app.exec_() ``` 在这个实现中,我们创建了一个名为 `MyWindow` 的作为我们的主窗口,并在其中创建了一个按钮一个标签用于显示图片。 当按钮被点击时,我们使用 QFileDialog 弹出一个文件选择框让用户选择需要打开的文件夹,并将所选文件夹路径传入 `showImages` 方法中。 在 `showImages` 方法中,我们使用 os 库遍历所选文件夹中的所有图片文件,并使用 Pillow 库中的 QPixmap 读取图片。每读取一张图片后,我们将其显示在标签控件中。 最后,在 `if __name__ == '__main__':` 中,我们创建了一个 QApplication 对象和一个 MyWindow 对象,并在主循环中开始应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值