python编写一个图片浏览器
时间: 2023-09-18 12:11:41 浏览: 83
下面是一个简单的 Python 图片浏览器的示例代码,使用了 PyQt5 库:
```python
import os
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QLabel, QAction
class ImageViewer(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("图片浏览器")
# 添加菜单栏
file_menu = self.menuBar().addMenu("文件")
open_file_action = QAction("打开文件", self)
open_file_action.setShortcut("Ctrl+O")
open_file_action.triggered.connect(self.open_file)
file_menu.addAction(open_file_action)
# 添加标签用于显示图片
self.image_label = QLabel()
self.image_label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(self.image_label)
def open_file(self):
file_path, _ = QFileDialog.getOpenFileName(self, "打开文件", os.path.expanduser("~"))
if file_path:
pixmap = QPixmap(file_path)
self.image_label.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication([])
image_viewer = ImageViewer()
image_viewer.show()
app.exec_()
```
运行代码后,将会弹出一个窗口,点击菜单栏的“打开文件”可以选择要浏览的图片。选中图片后,程序会将图片显示在窗口中央。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)