{"id":1181985,"date":"2025-01-15T18:58:07","date_gmt":"2025-01-15T10:58:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181985.html"},"modified":"2025-01-15T18:58:09","modified_gmt":"2025-01-15T10:58:09","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e7%95%8c%e9%9d%a2%e5%8a%a0%e8%bd%bd%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181985.html","title":{"rendered":"python\u5982\u4f55\u5728\u754c\u9762\u52a0\u8f7d\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130118\/5c5e9c24-5d1a-4b5f-ab53-a703925cab2d.webp\" alt=\"python\u5982\u4f55\u5728\u754c\u9762\u52a0\u8f7d\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u52a0\u8f7d\u56fe\u7247\u5230\u754c\u9762\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u6709\u4f7f\u7528Tkinter\u3001PyQt\u548cPygame\u7b49\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c<strong>Tkinter<\/strong>\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6700\u5e38\u7528\u4e5f\u6700\u6613\u4e8e\u4e0a\u624b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Tkinter\u52a0\u8f7d\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u5185\u7f6e\u7684\u56fe\u5f62\u754c\u9762\u5e93\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002\u52a0\u8f7d\u56fe\u7247\u4e3b\u8981\u4f7f\u7528<code>PhotoImage<\/code>\u6216<code>PIL<\/code>\uff08Python Imaging Library\uff09\u4e2d\u7684<code>Image<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528PhotoImage\u52a0\u8f7d\u56fe\u7247<\/h4>\n<\/p>\n<p><p><code>PhotoImage<\/code>\u7c7b\u53ef\u4ee5\u76f4\u63a5\u52a0\u8f7dGIF\u548cPPM\/PGM\u683c\u5f0f\u7684\u56fe\u7247\u3002\u5bf9\u4e8e\u5176\u4ed6\u683c\u5f0f\u7684\u56fe\u7247\uff0c\u9700\u8981\u501f\u52a9PIL\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Image Loader&quot;)<\/p>\n<p>    img = tk.PhotoImage(file=&quot;path_to_image.gif&quot;)<\/p>\n<p>    label = tk.Label(root, image=img)<\/p>\n<p>    label.pack()<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528PIL\u52a0\u8f7d\u56fe\u7247<\/h4>\n<\/p>\n<p><p>PIL\u5e93\u652f\u6301\u66f4\u591a\u7684\u56fe\u7247\u683c\u5f0f\u3002\u9700\u8981\u5148\u5b89\u88c5Pillow\u5e93\uff08PIL\u7684\u4e00\u4e2a\u5206\u652f\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from PIL import Image, ImageTk<\/p>\n<p>def main():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Image Loader&quot;)<\/p>\n<p>    img = Image.open(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img = ImageTk.PhotoImage(img)<\/p>\n<p>    label = tk.Label(root, image=img)<\/p>\n<p>    label.pack()<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001PyQt\u52a0\u8f7d\u56fe\u7247<\/h3>\n<\/p>\n<p><p>PyQt\u662fPython\u7ed1\u5b9a\u7684Qt\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u8de8\u5e73\u53f0\u5e94\u7528\u3002PyQt\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u5f62\u754c\u9762\u7ec4\u4ef6\uff0c\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247\u4e5f\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5PyQt5<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyqt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528PyQt5\u52a0\u8f7d\u56fe\u7247<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow<\/p>\n<p>from PyQt5.QtGui import QPixmap<\/p>\n<p>class ImageLoader(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&quot;Image Loader&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 800, 600)<\/p>\n<p>        label = QLabel(self)<\/p>\n<p>        pixmap = QPixmap(&quot;path_to_image.jpg&quot;)<\/p>\n<p>        label.setPixmap(pixmap)<\/p>\n<p>        self.setCentralWidget(label)<\/p>\n<p>def main():<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    loader = ImageLoader()<\/p>\n<p>    loader.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pygame\u52a0\u8f7d\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Pygame\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u7528\u4e8e\u5f00\u53d1\u89c6\u9891\u6e38\u620f\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u56fe\u5f62\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pygame<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Pygame\u52a0\u8f7d\u56fe\u7247<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<p>def main():<\/p>\n<p>    pygame.init()<\/p>\n<p>    screen = pygame.display.set_mode((800, 600))<\/p>\n<p>    pygame.display.set_caption(&quot;Image Loader&quot;)<\/p>\n<p>    img = pygame.image.load(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img_rect = img.get_rect(center=(400, 300))<\/p>\n<p>    running = True<\/p>\n<p>    while running:<\/p>\n<p>        for event in pygame.event.get():<\/p>\n<p>            if event.type == pygame.QUIT:<\/p>\n<p>                running = False<\/p>\n<p>        screen.fill((255, 255, 255))<\/p>\n<p>        screen.blit(img, img_rect)<\/p>\n<p>        pygame.display.flip()<\/p>\n<p>    pygame.quit()<\/p>\n<p>    sys.exit()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5c0f\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u52a0\u8f7d\u56fe\u7247\u5230\u754c\u9762\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1aTkinter\u3001PyQt\u548cPygame\u3002<\/strong> \u5176\u4e2d\uff0c<strong>Tkinter<\/strong>\u662f\u6700\u5e38\u7528\u548c\u6613\u4e8e\u4e0a\u624b\u7684\u9009\u62e9\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1b<strong>PyQt<\/strong>\u9002\u5408\u9700\u8981\u66f4\u591a\u529f\u80fd\u548c\u66f4\u590d\u6742\u754c\u9762\u7684\u5e94\u7528\uff1b<strong>Pygame<\/strong>\u5219\u9002\u5408\u5f00\u53d1\u6e38\u620f\u548c\u591a\u5a92\u4f53\u5e94\u7528\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u7247\u52a0\u8f7d\u548c\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0Tkinter\u4f7f\u7528PhotoImage\u52a0\u8f7d\u56fe\u7247\uff1a<\/h4>\n<\/p>\n<p><p><strong>Tkinter\u4e2d\u7684PhotoImage\u7c7b\u53ef\u4ee5\u76f4\u63a5\u52a0\u8f7dGIF\u548cPPM\/PGM\u683c\u5f0f\u7684\u56fe\u7247<\/strong>\uff0c\u5bf9\u4e8e\u5176\u4ed6\u683c\u5f0f\u7684\u56fe\u7247\u9700\u8981\u501f\u52a9PIL\u5e93\u3002PhotoImage\u7684\u4f7f\u7528\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2aTkinter\u7a97\u53e3\uff0c\u7136\u540e\u901a\u8fc7PhotoImage\u52a0\u8f7d\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230\u4e00\u4e2aLabel\u7ec4\u4ef6\u4e0a\uff0c\u6700\u540e\u901a\u8fc7pack()\u65b9\u6cd5\u5c06Label\u7ec4\u4ef6\u6dfb\u52a0\u5230\u7a97\u53e3\u4e2d\u5e76\u663e\u793a\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def main():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Image Loader&quot;)<\/p>\n<p>    img = tk.PhotoImage(file=&quot;path_to_image.gif&quot;)<\/p>\n<p>    label = tk.Label(root, image=img)<\/p>\n<p>    label.pack()<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u5bfc\u5165tkinter\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aTkinter\u7a97\u53e3\u3002\u63a5\u7740\uff0c\u901a\u8fc7PhotoImage\u7c7b\u52a0\u8f7d\u6307\u5b9a\u8def\u5f84\u7684GIF\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230\u4e00\u4e2aLabel\u7ec4\u4ef6\u4e0a\u3002\u6700\u540e\uff0c\u901a\u8fc7pack()\u65b9\u6cd5\u5c06Label\u7ec4\u4ef6\u6dfb\u52a0\u5230\u7a97\u53e3\u4e2d\uff0c\u5e76\u542f\u52a8Tkinter\u7684\u4e3b\u4e8b\u4ef6\u5faa\u73af\u663e\u793a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Tkinter\u52a0\u8f7d\u56fe\u7247\u7684\u66f4\u591a\u7ec6\u8282<\/h3>\n<\/p>\n<p><h4>1. Label\u7ec4\u4ef6\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728Tkinter\u4e2d\uff0cLabel\u7ec4\u4ef6\u662f\u7528\u4e8e\u663e\u793a\u6587\u672c\u6216\u56fe\u7247\u7684\u6700\u5e38\u7528\u7ec4\u4ef6\u4e4b\u4e00\u3002\u901a\u8fc7\u5c06\u56fe\u7247\u7ed1\u5b9a\u5230Label\u7ec4\u4ef6\u4e0a\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5728\u754c\u9762\u4e0a\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from PIL import Image, ImageTk<\/p>\n<p>def main():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Image Loader&quot;)<\/p>\n<p>    img = Image.open(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img = ImageTk.PhotoImage(img)<\/p>\n<p>    label = tk.Label(root, image=img)<\/p>\n<p>    label.pack()<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u8c03\u6574\u56fe\u7247\u7684\u5927\u5c0f\u4ee5\u9002\u5e94\u754c\u9762\u5e03\u5c40\u3002\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u4e2d\u7684resize()\u65b9\u6cd5\u5bf9\u56fe\u7247\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from PIL import Image, ImageTk<\/p>\n<p>def main():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Image Loader&quot;)<\/p>\n<p>    img = Image.open(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img = img.resize((400, 300), Image.ANTIALIAS)<\/p>\n<p>    img = ImageTk.PhotoImage(img)<\/p>\n<p>    label = tk.Label(root, image=img)<\/p>\n<p>    label.pack()<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u54cd\u5e94\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u56fe\u7247\u6dfb\u52a0\u4e8b\u4ef6\u54cd\u5e94\uff0c\u4f8b\u5982\u70b9\u51fb\u4e8b\u4ef6\u3002\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u4ea4\u4e92\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from PIL import Image, ImageTk<\/p>\n<p>def on_click(event):<\/p>\n<p>    print(&quot;Image clicked!&quot;)<\/p>\n<p>def main():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.title(&quot;Image Loader&quot;)<\/p>\n<p>    img = Image.open(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img = img.resize((400, 300), Image.ANTIALIAS)<\/p>\n<p>    img = ImageTk.PhotoImage(img)<\/p>\n<p>    label = tk.Label(root, image=img)<\/p>\n<p>    label.pack()<\/p>\n<p>    label.bind(&quot;&lt;Button-1&gt;&quot;, on_click)<\/p>\n<p>    root.mainloop()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528PyQt\u52a0\u8f7d\u56fe\u7247\u7684\u66f4\u591a\u7ec6\u8282<\/h3>\n<\/p>\n<p><h4>1. QLabel\u7ec4\u4ef6\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728PyQt\u4e2d\uff0cQLabel\u7ec4\u4ef6\u7528\u4e8e\u663e\u793a\u6587\u672c\u6216\u56fe\u7247\u3002\u901a\u8fc7\u5c06\u56fe\u7247\u52a0\u8f7d\u5230QPixmap\u5bf9\u8c61\u4e2d\uff0c\u5e76\u5c06\u5176\u7ed1\u5b9a\u5230QLabel\u7ec4\u4ef6\u4e0a\uff0c\u53ef\u4ee5\u5728\u754c\u9762\u4e0a\u663e\u793a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow<\/p>\n<p>from PyQt5.QtGui import QPixmap<\/p>\n<p>class ImageLoader(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&quot;Image Loader&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 800, 600)<\/p>\n<p>        label = QLabel(self)<\/p>\n<p>        pixmap = QPixmap(&quot;path_to_image.jpg&quot;)<\/p>\n<p>        label.setPixmap(pixmap)<\/p>\n<p>        self.setCentralWidget(label)<\/p>\n<p>def main():<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    loader = ImageLoader()<\/p>\n<p>    loader.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528QPixmap\u7684scaled()\u65b9\u6cd5\u8c03\u6574\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u4ee5\u9002\u5e94\u754c\u9762\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow<\/p>\n<p>from PyQt5.QtGui import QPixmap<\/p>\n<p>class ImageLoader(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&quot;Image Loader&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 800, 600)<\/p>\n<p>        label = QLabel(self)<\/p>\n<p>        pixmap = QPixmap(&quot;path_to_image.jpg&quot;)<\/p>\n<p>        pixmap = pixmap.scaled(400, 300)<\/p>\n<p>        label.setPixmap(pixmap)<\/p>\n<p>        self.setCentralWidget(label)<\/p>\n<p>def main():<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    loader = ImageLoader()<\/p>\n<p>    loader.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u54cd\u5e94\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u56fe\u7247\u6dfb\u52a0\u4e8b\u4ef6\u54cd\u5e94\uff0c\u4f8b\u5982\u70b9\u51fb\u4e8b\u4ef6\u3002\u901a\u8fc7\u91cd\u5199QLabel\u7ec4\u4ef6\u7684mousePressEvent()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u4ea4\u4e92\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow<\/p>\n<p>from PyQt5.QtGui import QPixmap<\/p>\n<p>from PyQt5.QtCore import Qt<\/p>\n<p>class ClickableLabel(QLabel):<\/p>\n<p>    def __init__(self, parent=None):<\/p>\n<p>        super().__init__(parent)<\/p>\n<p>    def mousePressEvent(self, event):<\/p>\n<p>        if event.button() == Qt.LeftButton:<\/p>\n<p>            print(&quot;Image clicked!&quot;)<\/p>\n<p>class ImageLoader(QMainWindow):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&quot;Image Loader&quot;)<\/p>\n<p>        self.setGeometry(100, 100, 800, 600)<\/p>\n<p>        label = ClickableLabel(self)<\/p>\n<p>        pixmap = QPixmap(&quot;path_to_image.jpg&quot;)<\/p>\n<p>        pixmap = pixmap.scaled(400, 300)<\/p>\n<p>        label.setPixmap(pixmap)<\/p>\n<p>        self.setCentralWidget(label)<\/p>\n<p>def main():<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    loader = ImageLoader()<\/p>\n<p>    loader.show()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Pygame\u52a0\u8f7d\u56fe\u7247\u7684\u66f4\u591a\u7ec6\u8282<\/h3>\n<\/p>\n<p><h4>1. \u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u5728Pygame\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pygame.image.load()\u65b9\u6cd5\u52a0\u8f7d\u56fe\u7247\uff0c\u5e76\u901a\u8fc7blit()\u65b9\u6cd5\u5c06\u56fe\u7247\u7ed8\u5236\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<p>def main():<\/p>\n<p>    pygame.init()<\/p>\n<p>    screen = pygame.display.set_mode((800, 600))<\/p>\n<p>    pygame.display.set_caption(&quot;Image Loader&quot;)<\/p>\n<p>    img = pygame.image.load(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img_rect = img.get_rect(center=(400, 300))<\/p>\n<p>    running = True<\/p>\n<p>    while running:<\/p>\n<p>        for event in pygame.event.get():<\/p>\n<p>            if event.type == pygame.QUIT:<\/p>\n<p>                running = False<\/p>\n<p>        screen.fill((255, 255, 255))<\/p>\n<p>        screen.blit(img, img_rect)<\/p>\n<p>        pygame.display.flip()<\/p>\n<p>    pygame.quit()<\/p>\n<p>    sys.exit()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pygame.transform.scale()\u65b9\u6cd5\u8c03\u6574\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u4ee5\u9002\u5e94\u754c\u9762\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<p>def main():<\/p>\n<p>    pygame.init()<\/p>\n<p>    screen = pygame.display.set_mode((800, 600))<\/p>\n<p>    pygame.display.set_caption(&quot;Image Loader&quot;)<\/p>\n<p>    img = pygame.image.load(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img = pygame.transform.scale(img, (400, 300))<\/p>\n<p>    img_rect = img.get_rect(center=(400, 300))<\/p>\n<p>    running = True<\/p>\n<p>    while running:<\/p>\n<p>        for event in pygame.event.get():<\/p>\n<p>            if event.type == pygame.QUIT:<\/p>\n<p>                running = False<\/p>\n<p>        screen.fill((255, 255, 255))<\/p>\n<p>        screen.blit(img, img_rect)<\/p>\n<p>        pygame.display.flip()<\/p>\n<p>    pygame.quit()<\/p>\n<p>    sys.exit()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u54cd\u5e94\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u56fe\u7247\u6dfb\u52a0\u4e8b\u4ef6\u54cd\u5e94\uff0c\u4f8b\u5982\u70b9\u51fb\u4e8b\u4ef6\u3002\u901a\u8fc7\u68c0\u6d4b\u9f20\u6807\u70b9\u51fb\u4e8b\u4ef6\u7684\u5750\u6807\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u4ea4\u4e92\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<p>def main():<\/p>\n<p>    pygame.init()<\/p>\n<p>    screen = pygame.display.set_mode((800, 600))<\/p>\n<p>    pygame.display.set_caption(&quot;Image Loader&quot;)<\/p>\n<p>    img = pygame.image.load(&quot;path_to_image.jpg&quot;)<\/p>\n<p>    img = pygame.transform.scale(img, (400, 300))<\/p>\n<p>    img_rect = img.get_rect(center=(400, 300))<\/p>\n<p>    running = True<\/p>\n<p>    while running:<\/p>\n<p>        for event in pygame.event.get():<\/p>\n<p>            if event.type == pygame.QUIT:<\/p>\n<p>                running = False<\/p>\n<p>            elif event.type == pygame.MOUSEBUTTONDOWN:<\/p>\n<p>                if img_rect.collidepoint(event.pos):<\/p>\n<p>                    print(&quot;Image clicked!&quot;)<\/p>\n<p>        screen.fill((255, 255, 255))<\/p>\n<p>        screen.blit(img, img_rect)<\/p>\n<p>        pygame.display.flip()<\/p>\n<p>    pygame.quit()<\/p>\n<p>    sys.exit()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u52a0\u8f7d\u56fe\u7247\u5230\u754c\u9762\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u6709\u4f7f\u7528Tkinter\u3001PyQt\u548cPygame\u7b49\u5e93\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>Tkinter<\/strong>\uff1a\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u6700\u5e38\u7528\u4e5f\u6700\u6613\u4e8e\u4e0a\u624b\u3002\u4f7f\u7528PhotoImage\u7c7b\u53ef\u4ee5\u76f4\u63a5\u52a0\u8f7dGIF\u548cPPM\/PGM\u683c\u5f0f\u7684\u56fe\u7247\uff0c\u4f7f\u7528PIL\u5e93\u53ef\u4ee5\u52a0\u8f7d\u66f4\u591a\u683c\u5f0f\u7684\u56fe\u7247\u3002<\/li>\n<li><strong>PyQt<\/strong>\uff1a\u662fPython\u7ed1\u5b9a\u7684Qt\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u8de8\u5e73\u53f0\u5e94\u7528\u3002\u901a\u8fc7QLabel\u7ec4\u4ef6\u548cQPixmap\u5bf9\u8c61\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u754c\u9762\u4e0a\u663e\u793a\u56fe\u7247\u3002<\/li>\n<li><strong>Pygame<\/strong>\uff1a\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u7528\u4e8e\u5f00\u53d1\u89c6\u9891\u6e38\u620f\u3002\u901a\u8fc7pygame.image.load()\u65b9\u6cd5\u52a0\u8f7d\u56fe\u7247\uff0c\u5e76\u901a\u8fc7blit()\u65b9\u6cd5\u5c06\u56fe\u7247\u7ed8\u5236\u5230\u5c4f\u5e55\u4e0a\u3002<\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u7247\u52a0\u8f7d\u548c\u663e\u793a\u6548\u679c\u3002\u901a\u8fc7\u8c03\u6574\u56fe\u7247\u5927\u5c0f\u548c\u6dfb\u52a0\u4e8b\u4ef6\u54cd\u5e94\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u4ea4\u4e92\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u754c\u9762\u4e2d\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\u6765\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Tkinter\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>PhotoImage<\/code>\u6216<code>PIL<\/code>\u5e93\u4e2d\u7684<code>ImageTk<\/code>\u6765\u5b9e\u73b0\u56fe\u7247\u7684\u52a0\u8f7d\u4e0e\u5c55\u793a\u3002\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Pillow\u5e93\uff08\u4f7f\u7528<code>pip install Pillow<\/code>\uff09\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\uff0c\u52a0\u8f7d\u56fe\u7247\u5e76\u4f7f\u7528\u6807\u7b7e\uff08Label\uff09\u7ec4\u4ef6\u6765\u663e\u793a\u3002<\/p>\n<p><strong>\u5728Python GUI\u4e2d\u652f\u6301\u54ea\u4e9b\u56fe\u7247\u683c\u5f0f\uff1f<\/strong><br \/>Python\u7684\u4e0d\u540cGUI\u5e93\u652f\u6301\u591a\u79cd\u56fe\u7247\u683c\u5f0f\u3002Tkinter\u7684<code>PhotoImage<\/code>\u4e3b\u8981\u652f\u6301GIF\u548cPPM\/PGM\u683c\u5f0f\uff0c\u800cPillow\u5e93\u5219\u652f\u6301\u66f4\u591a\u683c\u5f0f\uff0c\u5305\u62ecJPEG\u3001PNG\u3001BMP\u7b49\u3002\u56e0\u6b64\uff0c\u4f7f\u7528Pillow\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u591a\u79cd\u56fe\u7247\u683c\u5f0f\uff0c\u589e\u5f3a\u4e86\u5728\u754c\u9762\u4e2d\u52a0\u8f7d\u56fe\u7247\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u52a0\u8f7d\u5927\u56fe\u7247\u5728Python\u754c\u9762\u4e2d\u7684\u663e\u793a\u6027\u80fd\uff1f<\/strong><br \/>\u5904\u7406\u5927\u56fe\u7247\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f29\u5c0f\u56fe\u7247\u5c3a\u5bf8\u6216\u8005\u4f7f\u7528\u56fe\u50cf\u7684\u7f29\u7565\u56fe\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f7f\u7528Pillow\u5e93\u4e2d\u7684<code>thumbnail<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u52a0\u8f7d\u56fe\u7247\u65f6\u81ea\u52a8\u8c03\u6574\u5176\u5927\u5c0f\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u5e76\u63d0\u9ad8\u754c\u9762\u7684\u54cd\u5e94\u901f\u5ea6\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528\u5ef6\u8fdf\u52a0\u8f7d\u6216\u5206\u5757\u52a0\u8f7d\u6280\u672f\uff0c\u4ee5\u786e\u4fdd\u754c\u9762\u4e0d\u4f1a\u56e0\u52a0\u8f7d\u5927\u56fe\u7247\u800c\u53d8\u5f97\u5361\u987f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u52a0\u8f7d\u56fe\u7247\u5230\u754c\u9762\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u6709\u4f7f\u7528Tkinter\u3001PyQt\u548cPygame\u7b49\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1181991,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181985"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1181985"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181985\/revisions"}],"predecessor-version":[{"id":1181993,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181985\/revisions\/1181993"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181991"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}