Display local IP address and corresponding QR code, which is convenient for users to share and access network information

This code uses wxPython, socket, qrcode and PIL (Python Imaging Library) modules to generate a window application with a local IP address and corresponding QR code.
C:\pythoncode\new\showipgenqrcode.py
insert image description here

Let's explain what the code does line by line:

  1. import wx: Import the wxPython module, used to create windows and controls for GUI applications.
  2. import socket: Import the socket module to obtain the local IP address.
  3. import qrcode: Import the qrcode module for generating QR codes.
  4. from PIL import Image: Import Image class from PIL (Python Imaging Library) module, used to convert QR code to image file.

Next is to define a class MyFrame, inherited from wxPython wx.Frameclass, used to create window applications. In __init__the method:

  1. super().__init__(None, title="IP Address and QR Code", size=(400, 300)): Call the constructor of the parent class, create a window, and set the title and size.
  2. panel = wx.Panel(self): Create a panel for placing other controls.
  3. ip_address = socket.gethostbyname(socket.gethostname()): Use the socket module to get the local IP address.
  4. qr = qrcode.QRCode(version=1, box_size=10, border=4): Create a QRCode object, specify the version, square size and border width.
  5. qr.add_data(ip_address): Add the local IP address to the QRCode object.
  6. qr.make(fit=True): Generates a QR code image.
  7. qr_image = qr.make_image(fill_color="black", back_color="white"): Generate image according to QRCode object, set foreground color and background color.
  8. temp_file = "temp_qrcode.png": Specifies the name of the temporary file.
  9. qr_image.save(temp_file): Save the QR code image as a temporary file.
  10. qr_bitmap = wx.Bitmap(temp_file, type=wx.BITMAP_TYPE_PNG): Create a bitmap object using a temporary file.
  11. ip_label = wx.StaticText(panel, label=f"IP Address: {ip_address}"): Create a static text control that displays the IP address.
  12. qr_bitmap_ctrl = wx.StaticBitmap(panel, bitmap=qr_bitmap): Creates a static bitmap control that displays a QR code image.
  13. sizer = wx.BoxSizer(wx.VERTICAL): Creates a vertical layout manager.
  14. sizer.Add(ip_label, 0, wx.ALL, 10): Adds an IP address text control to the layout and sets the margins.
  15. sizer.Add(qr_bitmap_ctrl, 0, wx.ALL, 10): Add the QR code bitmap control to the layout and set the margin.
  16. panel.SetSizerAndFit(sizer): Applies a layout manager to the panel.

all codes

import wx
import socket
import qrcode
from PIL import Image

class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title="IP Address and QR Code", size=(400, 300))
        
        panel = wx.Panel(self)
        
        # 获取本地 IP 地址
        ip_address = socket.gethostbyname(socket.gethostname())
        
        # 生成 QR 码
        qr = qrcode.QRCode(version=1, box_size=10, border=4)
        qr.add_data(ip_address)
        qr.make(fit=True)
        
        # 将 QR 码转换为图片
        qr_image = qr.make_image(fill_color="black", back_color="white")
        
        # 将图片保存为临时文件
        temp_file = "temp_qrcode.png"
        qr_image.save(temp_file)
        
        # 创建位图对象
        qr_bitmap = wx.Bitmap(temp_file, type=wx.BITMAP_TYPE_PNG)
        
        # 在面板上显示 IP 地址和 QR 码
        ip_label = wx.StaticText(panel, label=f"IP Address: {
      
      ip_address}")
        qr_bitmap_ctrl = wx.StaticBitmap(panel, bitmap=qr_bitmap)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(ip_label, 0, wx.ALL, 10)
        sizer.Add(qr_bitmap_ctrl, 0, wx.ALL, 10)
        panel.SetSizerAndFit(sizer)

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

Finally, we if __name__ == '__main__':create an instance of the wxPython application in the block and run the application's main loop ( app.MainLoop()). This will display the window application and wait for user interaction.

The application window will display the local IP address and corresponding QR code, allowing users to share and access network information.

Guess you like

Origin blog.csdn.net/winniezhang/article/details/132611384