Hướng dẫn can you build a gui in python? - bạn có thể xây dựng một gui trong python không?

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Đơn giản hóa sự phát triển của GUI Python với Pysimplegui This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Simplify Python GUI Development With PySimpleGUI

Tạo giao diện người dùng đồ họa đơn giản (GUI) hoạt động trên nhiều nền tảng có thể phức tạp. Nhưng nó không phải là như vậy. Bạn có thể sử dụng gói Python và PysimpleGui để tạo giao diện người dùng đẹp mắt mà bạn và người dùng của bạn sẽ thích! Pysimplegui là một thư viện GUI Python mới đã nhận được rất nhiều sự quan tâm gần đây.

Trong hướng dẫn này, bạn sẽ học cách:

  • Cài đặt gói pysimplegui the PySimpleGUI package
  • Tạo các yếu tố giao diện người dùng cơ bản với pysimpleguiuser interface elements with PySimpleGUI
  • Tạo các ứng dụng, chẳng hạn như trình xem hình ảnh pysimpleguiimage viewer
  • Tích hợp pysimplegui với matplotlibMatplotlib
  • Sử dụng tầm nhìn máy tính trong pysimpleguicomputer vision in PySimpleGUI
  • Gói ứng dụng PysimpleGui của bạn cho WindowsWindows

Bây giờ là thời gian để bắt đầu!

Bắt đầu với Pysimplegui

Pysimplegui đã được ra mắt vào năm 2018, do đó, nó có một gói tương đối mới so với Wxpython hoặc PyQT.

Pysimplegui có bốn cổng:

  1. Tkinter
  2. Pyqt
  3. Wxpython
  4. Remi

Pysimplegui kết thúc các phần của mỗi gói này và làm cho chúng dễ sử dụng hơn. Tuy nhiên, mỗi cổng phải được cài đặt riêng.

Pysimplegui kết thúc toàn bộ Tkinter, đi kèm với Python. Pysimplegui đã bọc hầu hết pyside2, nhưng chỉ một phần nhỏ của wxpython. Khi bạn cài đặt pysimplegui, bạn sẽ nhận được biến thể Tkinter theo mặc định. Để biết thêm thông tin về Tkinter, hãy xem chương trình GUI Python với Tkinter.Tkinter variant by default. For more information about Tkinter, check out Python GUI Programming With Tkinter.

Tùy thuộc vào biến thể của pysimplegui bạn sử dụng, các ứng dụng mà bạn tạo với pysimplegui có thể không có nguồn gốc từ nền tảng của chúng. Nhưng đừng để điều này ngăn bạn thử Pysimplegui thử. Pysimplegui vẫn khá mạnh mẽ và có thể hoàn thành hầu hết mọi việc với một công việc nhỏ.

Cài đặt pysimplegui

Cài đặt pysimplegui là dễ dàng nếu bạn sử dụng PIP. Đối với các mục đích của hướng dẫn này, bạn sẽ học cách cài đặt cổng pysimplegui thông thường, đó là biến thể Tkinter.

Đây là cách làm điều đó:

$ python -m pip install pysimplegui

Điều này sẽ cài đặt pysimplegui cho bất cứ điều gì python hệ thống của bạn được đặt thành. Bạn cũng có thể cài đặt PysimpleGui vào môi trường ảo Python. Nếu bạn không quen thuộc với môi trường ảo Python, thì bạn nên đọc môi trường ảo Python: một mồi.

Nếu bạn thích thử biến thể PYQT, thì bạn có thể sử dụng

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
8 thay thế. Bây giờ bạn đã cài đặt pysimplegui, đã đến lúc tìm hiểu cách sử dụng nó!

Tạo các yếu tố UI cơ bản trong pysimplegui

Nếu bạn đã từng sử dụng bộ công cụ GUI trước đây, thì bạn có thể đã nghe thấy thuật ngữ này. Một tiện ích là một thuật ngữ chung được sử dụng để mô tả các yếu tố tạo nên giao diện người dùng (UI), chẳng hạn như nút, nhãn, cửa sổ, v.v. Trong pysimplegui, các vật dụng được gọi là các yếu tố, đôi khi bạn có thể thấy được viết hoa ở nơi khác là yếu tố.widgets. A widget is a generic term used to describe the elements that make up the user interface (UI), such as buttons, labels, windows, and more. In PySimpleGUI, widgets are referred to as elements, which you may sometimes see capitalized elsewhere as Elements.

Một trong những khối xây dựng cơ bản của pysimplegui là

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9. Để tạo
# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9, bạn có thể làm như sau:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9 có rất nhiều đối số khác nhau, rất nhiều đối số được liệt kê ở đây. Tuy nhiên, trong ví dụ này, bạn có thể cung cấp cho
# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9 A
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
3 và
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
4 và đặt
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
5, đó là cửa sổ UI sẽ lớn như thế nào trong pixel.

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
6 Trả về bất kỳ sự kiện nào được kích hoạt trong
# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9 dưới dạng chuỗi cũng như từ điển
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
8. Bạn sẽ tìm hiểu thêm về những điều này trong các phần sau của hướng dẫn này.

Khi bạn chạy mã này, bạn sẽ thấy một cái gì đó như thế này:

Hướng dẫn can you build a gui in python? - bạn có thể xây dựng một gui trong python không?

Ví dụ này không thực sự làm nhiều thứ khác ngoài việc có thể hiển thị thông điệp cho người dùng.

Thông thường, bạn sẽ có các yếu tố khác ngoài

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9 trong ứng dụng của mình, vì vậy hãy để thêm một số văn bản và một nút vào hỗn hợp.

Tạo một tệp mới có tên

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
0 và thêm mã này:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()

Hầu hết các bộ công cụ GUI cho phép bạn đặt ra các yếu tố bằng cách sử dụng định vị tuyệt đối hoặc bằng cách cho phép GUI đặt chúng ra một cách linh hoạt. Ví dụ: Wxpython sử dụng

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
1 để bố trí các yếu tố một cách linh hoạt. Nếu bạn muốn tìm hiểu thêm về WxPython, thì hãy xem cách xây dựng ứng dụng GUI Python với WxPython.absolute positioning or by allowing the GUI to lay them out dynamically. For example, wxPython uses
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
1 to lay out elements dynamically. If you’d like to learn more about wxPython, then check out How to Build a Python GUI Application With wxPython.

Pysimplegui sử dụng danh sách python lồng nhau để đưa ra các yếu tố của nó. Trong trường hợp này, bạn thêm phần tử

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
2 và phần tử
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
3. Sau đó, bạn tạo
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
4 và vượt qua trong tùy chỉnh của bạn.

Khối mã cuối cùng là vòng lặp sự kiện. Giao diện người dùng đồ họa cần chạy bên trong một vòng lặp và chờ người dùng làm điều gì đó. Ví dụ: người dùng có thể cần nhấn một nút trong giao diện người dùng của bạn hoặc nhập một cái gì đó bằng bàn phím của họ. Khi họ làm điều đó, những sự kiện đó được xử lý bởi vòng lặp sự kiện.event loop. A graphical user interface needs to run inside a loop and wait for the user to do something. For example, the user might need to press a button in your UI or type something with their keyboard. When they do that, those events are processed by the event loop.

Khi bạn sử dụng pysimplegui, bạn tạo một vòng lặp sự kiện bằng cách tạo vòng lặp

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
6 vô hạn đọc các sự kiện từ đối tượng
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
4. Nếu người dùng nhấn nút
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
8 hoặc nút thoát, thì bạn muốn chương trình kết thúc. Để thực hiện điều đó, bạn
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
9 ra khỏi vòng lặp và
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
0
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
4.

Mã trên tạo ra một ứng dụng trông như thế này:

Hướng dẫn can you build a gui in python? - bạn có thể xây dựng một gui trong python không?

Bây giờ bạn đã sẵn sàng để tạo một ứng dụng thực tế!

Tạo các ứng dụng đơn giản

Bạn có thể tạo ra một loạt các GUI đa nền tảng khác nhau bằng pysimplegui. Các bản demo được bao gồm trong gói rất rộng. Bạn có thể tạo bất cứ thứ gì từ các tiện ích máy tính để bàn đến giao diện người dùng toàn diện.

Trong một vài phần tiếp theo, bạn sẽ thấy một vài cách khác nhau để sử dụng pysimplegui. Tuy nhiên, có rất nhiều điều mà bạn có thể làm ngoài những gì có thể được đề cập trong một hướng dẫn duy nhất. Nếu bạn muốn thêm chi tiết, hãy chắc chắn kiểm tra các bản demo khác được bao gồm trong Pysimplegui.

Tạo trình xem hình ảnh pysimplegui

Một trong những bản demo trên trang Pysimplegui từ GitHub là một trình xem hình ảnh. Có thể viết trình xem hình ảnh tùy chỉnh của riêng bạn với Python rất thú vị. Bạn có thể sử dụng mã này để xem ảnh của riêng bạn hoặc kết hợp nó để xem ảnh mà bạn tải xuống hoặc đọc từ cơ sở dữ liệu.

Để giữ cho mọi thứ đơn giản, bạn sẽ sử dụng phần tử

21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2 tích hợp PysimpleGui, để xem hình ảnh. Thật không may, phần tử
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2 chỉ có thể hiển thị các định dạng PNG và GIF trong phiên bản thông thường của pysimplegui.

Nếu bạn muốn có thể mở các loại tệp hình ảnh khác, thì bạn có thể tải xuống Gối, hỗ trợ các định dạng TIFF, JPG và BMP. Kiểm tra thư mục demo pysimplegui trên GitHub để biết ví dụ cho thấy cách thực hiện việc này.

Mặt khác, nếu bạn cài đặt cổng pysimpleguiqt, thì bạn sẽ thấy rằng QT hỗ trợ nhiều định dạng hình ảnh ra khỏi hộp hơn Tkinter.

Ở đây, một mô hình về những gì người xem hình ảnh sẽ trông như thế nào ở phần cuối:

Hướng dẫn can you build a gui in python? - bạn có thể xây dựng một gui trong python không?

Sẽ có rất nhiều mã cho ví dụ này, nhưng đừng lo lắng. Bạn sẽ đi qua nó trong những khối nhỏ hơn sau đó.

Bạn có thể tạo một tệp có tên

21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
4 trong trình soạn thảo Python của bạn. Sau đó thêm mã sau:

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()

Phew! Đó là một vài dòng mã! Hãy cùng đi qua từng mảnh.

Đây là một vài dòng đầu tiên:

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]

Ở đây, trên các dòng 3 và 4, bạn nhập mô -đun

21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
5 và Python từ
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
6. Sau đó, trên các dòng 8 đến 19, bạn tạo một danh sách các phần tử lồng nhau đại diện cho một cột dọc của giao diện người dùng. Điều này sẽ tạo ra một nút duyệt mà bạn sẽ sử dụng để tìm một thư mục có hình ảnh trong đó.lines 3 and 4, you import
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
5 and Python’s
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
6 module. Then, on lines 8 through 19, you create a nested list of elements that represent a vertical column of the user interface. This will create a Browse button that you’ll use to find a folder that has images in it.

Tham số

21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
7 là quan trọng. Đây là những gì bạn sử dụng để xác định một yếu tố cụ thể trong GUI của bạn. Đối với điều khiển văn bản đầu vào
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
8, bạn cung cấp cho nó một danh tính là
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
9. Bạn sẽ sử dụng điều này sau để truy cập vào nội dung của phần tử. Bạn có thể bật hoặc tắt các sự kiện cho từng phần tử thông qua tham số
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
0.

Phần tử

28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
1 sẽ hiển thị danh sách các đường dẫn đến các hình ảnh mà sau đó bạn có thể chọn để hiển thị. Bạn có thể đặt trước
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
1 với các giá trị bằng cách truyền trong danh sách các chuỗi.

Khi bạn lần đầu tiên tải lên giao diện người dùng của mình, bạn muốn

28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
1 trống, vì vậy bạn vượt qua danh sách trống. Bạn bật các sự kiện cho phần tử này, đặt
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
4 của nó và cung cấp cho nó một định danh duy nhất giống như bạn đã làm với phần tử đầu vào.

Bây giờ bạn có thể nhìn vào cột các yếu tố bên phải:

21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]

Danh sách các danh sách trên các dòng từ 22 đến 26 tạo ra ba yếu tố. Phần tử đầu tiên cho người dùng biết rằng họ nên chọn một hình ảnh để hiển thị. Phần tử thứ hai hiển thị tên của tệp đã chọn. Thứ ba hiển thị

21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2.lines 22 through 26 creates three elements. The first element tells the user that they should choose an image to display. The second element displays the name of the selected file. The third displays the
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2.

Lưu ý rằng phần tử

21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2 cũng có bộ
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
7 để bạn có thể dễ dàng tham khảo lại phần tử sau. Để biết thêm thông tin về yếu tố
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2, hãy xem tài liệu.

Phần mã tiếp theo xác định bố cục của bạn:

28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]

Danh sách cuối cùng, trên các dòng 29 đến 35, chứa mã kiểm soát cách các yếu tố được đặt ra trên màn hình. Mã này chứa hai phần tử

28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
9 với
37window = sg.Window("Image Viewer", layout)
0 giữa chúng.
37window = sg.Window("Image Viewer", layout)
0 là bí danh cho
37window = sg.Window("Image Viewer", layout)
2. Bạn có thể tìm hiểu thêm về cách
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
9 và
37window = sg.Window("Image Viewer", layout)
0 hoạt động bằng cách đọc các trang tài liệu tương ứng của họ.lines 29 to 35, contains the code that controls how the elements are laid out on the screen. This code contains two
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
9 elements with a
37window = sg.Window("Image Viewer", layout)
0 between them.
37window = sg.Window("Image Viewer", layout)
0 is an alias for
37window = sg.Window("Image Viewer", layout)
2. You can learn more about how
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
9 and
37window = sg.Window("Image Viewer", layout)
0 work by reading their respective documentation pages.

Để thêm

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
4 vào
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
4 của bạn, bạn có thể làm điều này:

37window = sg.Window("Image Viewer", layout)

Bây giờ bạn đã tìm ra giao diện người dùng, bạn có thể xem mã vòng lặp sự kiện. Ở đây, phần đầu tiên:

39while True:
40    event, values = window.read()
41    if event == "Exit" or event == sg.WIN_CLOSED:
42        break

Vòng lặp sự kiện chứa logic của chương trình của bạn. Tại đây, bạn trích xuất các sự kiện và

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
8 từ
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
4.
37window = sg.Window("Image Viewer", layout)
9 sẽ là chuỗi
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
7 của bất kỳ phần tử nào mà người dùng tương tác. Biến
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
8 chứa từ điển Python ánh xạ phần tử
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
7 thành một giá trị. Ví dụ: nếu người dùng chọn một thư mục, thì
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
9 sẽ ánh xạ tới đường dẫn thư mục.logic of your program. Here, you extract the events and
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
8 from the
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
4. The
37window = sg.Window("Image Viewer", layout)
9 will be the
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
7 string of whichever element the user interacts with. The
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
8 variable contains a Python dictionary that maps the element
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
7 to a value. For example, if the user picks a folder, then
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
9 will map to the folder path.

Các tuyên bố có điều kiện được sử dụng để kiểm soát những gì xảy ra. Nếu

37window = sg.Window("Image Viewer", layout)
9 bằng
39while True:
40    event, values = window.read()
41    if event == "Exit" or event == sg.WIN_CLOSED:
42        break
5 hoặc người dùng đóng
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
4, thì bạn thoát ra khỏi vòng lặp.

Bây giờ bạn có thể xem phần đầu tiên của câu lệnh có điều kiện tiếp theo trong vòng lặp:

44# Folder name was filled in, make a list of files in the folder
45if event == "-FOLDER-":
46    folder = values["-FOLDER-"]
47    try:
48        # Get list of files in folder
49        file_list = os.listdir(folder)
50    except:
51        file_list = []
52
53    fnames = [
54        f
55        for f in file_list
56        if os.path.isfile(os.path.join(folder, f))
57        and f.lower().endswith((".png", ".gif"))
58    ]
59    window["-FILE LIST-"].update(fnames)

Lần này bạn kiểm tra

37window = sg.Window("Image Viewer", layout)
9 so với
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
9
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
7, trong đó đề cập đến yếu tố
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
8 mà bạn đã tạo trước đó. Nếu sự kiện tồn tại, thì bạn biết người dùng đã chọn một thư mục và bạn sử dụng
44# Folder name was filled in, make a list of files in the folder
45if event == "-FOLDER-":
46    folder = values["-FOLDER-"]
47    try:
48        # Get list of files in folder
49        file_list = os.listdir(folder)
50    except:
51        file_list = []
52
53    fnames = [
54        f
55        for f in file_list
56        if os.path.isfile(os.path.join(folder, f))
57        and f.lower().endswith((".png", ".gif"))
58    ]
59    window["-FILE LIST-"].update(fnames)
1 để có được danh sách tệp. Sau đó, bạn lọc danh sách đó xuống chỉ các tệp có tiện ích mở rộng
44# Folder name was filled in, make a list of files in the folder
45if event == "-FOLDER-":
46    folder = values["-FOLDER-"]
47    try:
48        # Get list of files in folder
49        file_list = os.listdir(folder)
50    except:
51        file_list = []
52
53    fnames = [
54        f
55        for f in file_list
56        if os.path.isfile(os.path.join(folder, f))
57        and f.lower().endswith((".png", ".gif"))
58    ]
59    window["-FILE LIST-"].update(fnames)
2 hoặc
44# Folder name was filled in, make a list of files in the folder
45if event == "-FOLDER-":
46    folder = values["-FOLDER-"]
47    try:
48        # Get list of files in folder
49        file_list = os.listdir(folder)
50    except:
51        file_list = []
52
53    fnames = [
54        f
55        for f in file_list
56        if os.path.isfile(os.path.join(folder, f))
57        and f.lower().endswith((".png", ".gif"))
58    ]
59    window["-FILE LIST-"].update(fnames)
3.

Bây giờ bạn có thể xem phần tiếp theo của câu lệnh có điều kiện:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
0

Nếu

37window = sg.Window("Image Viewer", layout)
9 bằng
44# Folder name was filled in, make a list of files in the folder
45if event == "-FOLDER-":
46    folder = values["-FOLDER-"]
47    try:
48        # Get list of files in folder
49        file_list = os.listdir(folder)
50    except:
51        file_list = []
52
53    fnames = [
54        f
55        for f in file_list
56        if os.path.isfile(os.path.join(folder, f))
57        and f.lower().endswith((".png", ".gif"))
58    ]
59    window["-FILE LIST-"].update(fnames)
5, thì bạn sẽ biết người dùng đã chọn một tệp trong
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
1 và bạn muốn cập nhật phần tử
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2 cũng như phần tử
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
2 hiển thị
44# Folder name was filled in, make a list of files in the folder
45if event == "-FOLDER-":
46    folder = values["-FOLDER-"]
47    try:
48        # Get list of files in folder
49        file_list = os.listdir(folder)
50    except:
51        file_list = []
52
53    fnames = [
54        f
55        for f in file_list
56        if os.path.isfile(os.path.join(folder, f))
57        and f.lower().endswith((".png", ".gif"))
58    ]
59    window["-FILE LIST-"].update(fnames)
9 đã chọn ở bên phải.

Bit cuối cùng của mã là cách bạn kết thúc chương trình:

Khi người dùng nhấn nút thoát, ứng dụng phải đóng. Để làm điều đó, bạn có thể sử dụng

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
00.

Về mặt kỹ thuật, bạn có thể loại bỏ dòng này khỏi mã của mình và Python vẫn sẽ kết thúc chương trình, nhưng nó luôn luôn là một ý tưởng tốt để tự mình dọn dẹp. Ngoài ra, nếu bạn sử dụng cổng web của Pysimplegui và bạn không đóng cửa sổ đúng cách, thì cuối cùng bạn sẽ rời khỏi một cổng mở.

Bây giờ hãy chạy mã và bạn sẽ thấy một giao diện như thế này:

Hướng dẫn can you build a gui in python? - bạn có thể xây dựng một gui trong python không?

Bạn có thể sử dụng nút Duyệt để tìm thư mục trên máy tính của bạn với hình ảnh trong đó để bạn có thể thử mã này. Hoặc bạn có thể sao chép và dán một đường dẫn vào một tệp vào phần tử

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
2.

Khi bạn đã hoàn thành việc xem hình ảnh của mình, bạn đã sẵn sàng tìm hiểu cách sử dụng matplotlib với pysimplegui.

Tích hợp matplotlib với pysimplegui

Tạo biểu đồ là một cách tuyệt vời để chia sẻ thông tin với các đồng nghiệp của bạn. Một trong những gói đồ thị phổ biến nhất cho Python là matplotlib. Matplotlib có thể tạo ra tất cả các loại đồ thị khác nhau. Nếu bạn muốn biết thêm về nó, hãy xem Python âm mưu với matplotlib (Hướng dẫn).

Matplotlib có thể được tích hợp với pysimplegui, vì vậy bạn có thể thêm đồ thị vào GUI của bạn khá dễ dàng nếu bạn đã biết cách sử dụng matplotlib.

Nếu bạn không cài đặt matplotlib, thì bạn có thể làm như vậy bằng cách sử dụng

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
02:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
1

Ví dụ này, bạn sử dụng một trong những bản demo Pysimplegui. Matplotlib sử dụng Numpy, vì vậy bạn cũng sẽ muốn cài đặt nó:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
2

Bây giờ bạn có tất cả các phần bạn cần để viết mã, bạn có thể tạo một tệp mới và đặt tên cho nó

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
03.

Mã demo dài một chút, vì vậy bạn thêm mã thành từng mảnh bắt đầu với điều này:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
3

Đây là những hàng nhập khẩu mà bạn sẽ cần để làm cho mã của bạn hoạt động. Mã này cũng thiết lập matplotlib

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
04 và thêm một lô bằng cách sử dụng
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
05. Để biết thêm chi tiết, bạn có thể muốn tham khảo tài liệu. Cổng Pysimpleguiqt hiện không hoạt động theo cách tương tự, nhưng điều đó đã được làm việc để phát hành trong tương lai.

Trong cả Pysimplegui và Tkinter, bạn sử dụng phần tử

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
06 để vẽ. Bạn có thể đọc thêm về yếu tố này trong tài liệu.

Bây giờ bạn có thể tạo một chức năng trợ giúp để vẽ hình trên Pysimplegui từ

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
06. Một hàm trợ giúp là mã lặp đi lặp lại mà bạn không muốn viết nhiều lần. Hãy xem:helper function is repetitive code that you don’t want to write multiple times. Take a look:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
4

Bạn sẽ sử dụng

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
08 để vẽ các lô đến phần tử pysimplegui từ ____106.

Để sử dụng pysimplegui từ

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
06, bạn cần chuyển nó vào
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
11 cùng với đối tượng matplotlib
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
12.
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
11 đến từ matplotlib và được sử dụng bởi tkinter để nhúng các lô trong pysimplegui. Điều này sẽ hoạt động khác nhau nếu bạn đang sử dụng pysimpleguiqt.

Bước cuối cùng là viết giao diện người dùng bằng pysimplegui:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
5

Để tạo giao diện người dùng, tất cả những gì bạn cần là phần tử

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
2, phần tử
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
06 và phần tử
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
3. Sau đó, bạn thêm tất cả những thứ đó vào
# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9 và gọi chức năng trợ giúp
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
18 của bạn để vẽ cốt truyện.

Bạn không cần vòng lặp sự kiện ở đây vì bạn đã thắng được tương tác với các yếu tố trong giao diện người dùng này.

Đây là những gì đồ thị sẽ trông như thế nào:

Hướng dẫn can you build a gui in python? - bạn có thể xây dựng một gui trong python không?

Có những bản demo matplotlib khác đi kèm với pysimplegui mà bạn nên kiểm tra.

Bây giờ bạn có thể tìm hiểu cách sử dụng opencv với pysimplegui.

Tích hợp opencv với pysimplegui

Tầm nhìn máy tính là một chủ đề nóng ngay bây giờ. Python cho phép bạn có được tầm nhìn máy tính bằng cách sử dụng gói OpenCV-Python, đây là một trình bao bọc xung quanh ứng dụng OpenCV phổ biến. Nếu bạn quan tâm đến việc tìm hiểu thêm về tầm nhìn máy tính, thì hãy xem nhận dạng khuôn mặt với Python, trong dưới 25 dòng mã.OpenCV application. If you’re interested in learning more about computer vision, then check out Face Recognition with Python, in Under 25 Lines of Code.

Pysimplegui có sự tích hợp đơn giản với thư viện OpenCV. Tuy nhiên, trước tiên bạn cần cài đặt OpenCV bằng

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
02:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
6

Bây giờ bạn đã cài đặt OpenCV, bạn có thể viết một ứng dụng thú vị!

Bạn sẽ nhìn vào một bản demo Pysimplegui khác sử dụng OpenCV và webcam máy tính của bạn. Ứng dụng này sẽ cho phép bạn áp dụng một số bộ lọc phổ biến cho video của mình trong thời gian thực.

Mã cho ví dụ này là dài, nhưng không cần phải lo lắng. Nó sẽ được giải thích trong các khối nhỏ hơn sau đó. Đi trước và tạo một tệp có tên

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
20 và thêm mã sau:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
7

Đó là một đoạn mã dài! Hãy cùng đi qua một ví dụ một mảnh tại một thời điểm:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
8

Các dòng 1 đến 3 là nhập khẩu cho các thư viện Python mà bạn sẽ cần. Sau đó, bạn đặt

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
21 trên dòng 6. are the imports for the Python libraries that you’ll need. Then you set the
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
21 on line 6.

Bước tiếp theo, bắt đầu trên dòng 9, là tạo

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
4 cho tất cả các yếu tố trong GUI. Bộ phần tử đầu tiên bao gồm phần tử
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
2, phần tử
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2 và phần tử
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
25. Bạn đặt khóa định danh cho phần tử
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
26 thành
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
27. Bạn cũng làm tổ một phần tử
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
25 và phần tử
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
29 và đặt các khóa định danh của chúng thành
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
30 và
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
31, tương ứng.line 9, is to create a
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
4 for all the elements in the GUI. The first set of elements includes a
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
2 element, an
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
2 element, and a
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
25 element. You set the identifier key for the
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
26 element to
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
27. You also nest a
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
25 element and a
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
29 element and set their identifier keys to
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
30 and
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
31, respectively.

Bây giờ bạn sẽ thêm một số yếu tố khác vào

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
4:

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
9

Trên các dòng 24 đến 42, bạn thêm một phần tử

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
25 khác và hai phần tử
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
29 để kiểm soát phát hiện cạnh Canny với giao diện người dùng. Bạn cũng đặt số nhận dạng một cách thích hợp.lines 24 through 42, you add another
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
25 element and two
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
29 elements to control canny edge detection with the user interface. You also set the identifiers appropriately.

Bây giờ bạn sẽ thêm một cách để làm mờ hình ảnh:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
0

Ở đây, bạn chỉ cần thêm một vài yếu tố để kiểm soát mờ hình ảnh, còn được gọi là làm mịn hình ảnh. Bạn có thể đọc thêm về kỹ thuật này trong tài liệu OpenCV.image blurring, which is also known as image smoothing. You can read more about this technique in the OpenCV documentation.

Bạn chỉ có thêm hai bộ điều khiển để thêm. Bạn sẽ thêm các điều khiển màu sắc tiếp theo:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
1

Các yếu tố này cho phép bạn chuyển đổi giữa các không gian màu khác nhau. Không gian màu nằm ngoài phạm vi của hướng dẫn này, nhưng bạn có thể đọc thêm về chúng trong hướng dẫn thay đổi màu sắc trên trang web OpenCV.color spaces. Color spaces are outside the scope of this tutorial, but you can read more about them in the Changing Colorspaces tutorial over on the OpenCV website.

Các yếu tố cuối cùng cần thêm là để kiểm soát độ tương phản:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
2

Một vài yếu tố cuối cùng này sẽ cho phép bạn tăng cường độ tương phản của luồng video bằng cách sử dụng thuật toán cân bằng biểu đồ thích ứng giới hạn tương phản.

Điều này kết thúc

 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
4. Sau đó, bạn chuyển
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
4 của bạn đến
# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
9 để bạn có thể xem UI trên màn hình của mình.

Cuối cùng, bạn sử dụng

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
38 để truy cập webcam trên máy của bạn. Bạn có thể thấy một cửa sổ bật lên yêu cầu bạn cho phép sử dụng máy ảnh của bạn. Nếu bạn làm như vậy, thì bạn sẽ cần phải cấp phép, nếu không mã này giành được công việc.

Bây giờ hãy xem phần còn lại của mã:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
3

Đây là vòng lặp sự kiện cho giao diện pysimplegui của bạn. Khi bạn thay đổi một thanh trượt trong UI của mình, PysimpleGui sẽ lấy

37window = sg.Window("Image Viewer", layout)
9 và
 1# img_viewer.py
 2
 3import PySimpleGUI as sg
 4import os.path
 5
 6# First the window layout in 2 columns
 7
 8file_list_column = [
 9    [
10        sg.Text("Image Folder"),
11        sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
12        sg.FolderBrowse(),
13    ],
14    [
15        sg.Listbox(
16            values=[], enable_events=True, size=(40, 20), key="-FILE LIST-"
17        )
18    ],
19]
20
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
27
28# ----- Full layout -----
29layout = [
30    [
31        sg.Column(file_list_column),
32        sg.VSeperator(),
33        sg.Column(image_viewer_column),
34    ]
35]
36
37window = sg.Window("Image Viewer", layout)
38
39# Run the Event Loop
40while True:
41    event, values = window.read()
42    if event == "Exit" or event == sg.WIN_CLOSED:
43        break
44    # Folder name was filled in, make a list of files in the folder
45    if event == "-FOLDER-":
46        folder = values["-FOLDER-"]
47        try:
48            # Get list of files in folder
49            file_list = os.listdir(folder)
50        except:
51            file_list = []
52
53        fnames = [
54            f
55            for f in file_list
56            if os.path.isfile(os.path.join(folder, f))
57            and f.lower().endswith((".png", ".gif"))
58        ]
59        window["-FILE LIST-"].update(fnames)
60    elif event == "-FILE LIST-":  # A file was chosen from the listbox
61        try:
62            filename = os.path.join(
63                values["-FOLDER-"], values["-FILE LIST-"][0]
64            )
65            window["-TOUT-"].update(filename)
66            window["-IMAGE-"].update(filename=filename)
67
68        except:
69            pass
70
71window.close()
8 và sử dụng chúng để xác định chức năng OpenCV nào để áp dụng cho luồng webcam của bạn.

Mã này khác một chút so với các mã khác mà bạn đã thấy bởi vì nó được gói gọn trong hàm

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
41. Loại chức năng này được sử dụng làm điểm nhập chính của chương trình. Để tìm hiểu thêm về chủ đề này, hãy xem xác định các chức năng chính trong Python.

Ở đây, một ví dụ về GUI sẽ trông như thế nào:

Hướng dẫn can you build a gui in python? - bạn có thể xây dựng một gui trong python không?

Bây giờ, thời gian học cách tạo ra một ứng dụng thực thi của bạn cho Windows.

Đóng gói ứng dụng pysimplegui của bạn cho Windows

Có nhiều gói Python khác nhau mà bạn có thể sử dụng để chuyển đổi mã Python của mình thành một thực thi cho Windows. Một trong những phổ biến nhất là Pyinstaller.

Bạn có thể cài đặt pyinstaller bằng

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
02:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
4

Để tìm hiểu thêm về cách sử dụng pyinstaller, hãy xem sử dụng pyinstaller để dễ dàng phân phối các ứng dụng Python.

Bạn sẽ sử dụng pyinstaller để chuyển đổi ứng dụng Trình xem hình ảnh mà bạn đã tạo trước đó thành một thực thi.

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
5

Khi bạn chạy lệnh này, bạn sẽ thấy rất nhiều đầu ra tương tự như sau:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
6

Đầu ra này được viết tắt vì pyinstaller khá dài dòng. Khi nó hoàn thành, bạn sẽ có một thư mục con có tên

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
43 trong cùng một thư mục có chứa
21# For now will only show the name of the file that was chosen
22image_viewer_column = [
23    [sg.Text("Choose an image from list on left:")],
24    [sg.Text(size=(40, 1), key="-TOUT-")],
25    [sg.Image(key="-IMAGE-")],
26]
4. Bạn có thể vào thư mục
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
43 để tìm
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
46 và thử chạy nó.

Sẽ có rất nhiều tệp khác trong thư mục

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
43 mà thực thi sử dụng.

Nếu bạn chỉ muốn có một tệp thực thi duy nhất, thì bạn có thể chạy lại lệnh bằng cờ

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
48:

# hello_psg.py

import PySimpleGUI as sg

layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button("OK")]]

# Create the window
window = sg.Window("Demo", layout)

# Create an event loop
while True:
    event, values = window.read()
    # End program if user closes window or
    # presses the OK button
    if event == "OK" or event == sg.WIN_CLOSED:
        break

window.close()
7

Điều này vẫn sẽ tạo ra thư mục

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
43, nhưng lần này sẽ có một thực thi duy nhất trong đó.

Khi bạn chạy thực thi, bạn sẽ thấy một cửa sổ bảng điều khiển xuất hiện ngoài giao diện người dùng của bạn. Để loại bỏ bảng điều khiển, bạn có thể sử dụng cờ

# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
50 hoặc
# hello_world.py

import PySimpleGUI as sg

sg.Window(title="Hello World", layout=[[]], margins=(100, 50)).read()
51 khi chạy pyinstaller.

Sự kết luận

Bạn đã học được rất nhiều về gói pysimplegui trong hướng dẫn này! Trên hết, bạn trở nên quen thuộc với những điều cơ bản của việc sử dụng PysimpleGui để tạo các ứng dụng.

Trong hướng dẫn này, bạn đã học được cách:

  • Cài đặt gói pysimplegui the PySimpleGUI package
  • Tạo các yếu tố giao diện người dùng cơ bản với pysimpleguiuser interface elements with PySimpleGUI
  • Tạo một số ứng dụng, chẳng hạn như trình xem hình ảnh, với pysimpleguiapplications, such as an image viewer, with PySimpleGUI
  • Tích hợp pysimplegui với matplotlibMatplotlib
  • Sử dụng tầm nhìn máy tính trong pysimpleguicomputer vision in PySimpleGUI
  • Gói ứng dụng PysimpleGui của bạn cho WindowsWindows

Bạn có thể sử dụng những gì bạn đã học trong hướng dẫn này để tạo các ứng dụng thú vị và hữu ích của riêng bạn.

Gói Pysimplegui cung cấp nhiều bản demo ví dụ mà bạn có thể sử dụng để xây dựng các kỹ năng của mình và khám phá cách sử dụng tất cả các công cụ trong thư viện hiệu quả hơn. Hãy chắc chắn kiểm tra chúng và bạn sẽ sớm thấy mình tạo ra các ứng dụng GUI đa nền tảng của riêng mình

Nếu bạn muốn tìm hiểu thêm về Pysimplegui, thì bạn có thể xem bất kỳ tài nguyên nào sau đây:

  • Các tài liệu Pysimplegui
  • Cookbook Pysimplegui
  • Các bản demo pysimplegui

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Đơn giản hóa sự phát triển của GUI Python với Pysimplegui This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Simplify Python GUI Development With PySimpleGUI

Bạn có thể làm GUI với Python không?

Tạo giao diện người dùng đồ họa đơn giản (GUI) hoạt động trên nhiều nền tảng có thể phức tạp. Nhưng nó không phải là như vậy. Bạn có thể sử dụng gói Python và PysimpleGui để tạo giao diện người dùng đẹp mắt mà bạn và người dùng của bạn sẽ thích!You can use Python and the PySimpleGUI package to create nice-looking user interfaces that you and your users will enjoy!

Làm cách nào để tạo một chương trình GUI trong Python?

Sử dụng tkinter để tạo GUI nếu bạn chưa quen với việc tạo GUI trong Python Các bước cơ bản là: Nhập mô -đun Tkinter: Nhập Tkinter, Top = Tkinter.tk () Tạo cửa sổ chính GUI sẽ là nhà của bạn GUI và GUI của bạn và Widgets. Thêm bất kỳ vật dụng nào mà GUI cần của bạn bao gồm các nút, nhãn, danh sách, v.v. If you're new to creating GUI's in Python the basic steps are to: Import the Tkinter module: import Tkinter, top = Tkinter.Tk() Create the GUI main window that will “house” your GUI and widgets. Add whatever widgets your GUI needs including buttons, labels, lists etc.

Python có tốt cho GUI máy tính để bàn không?

Ngày nay, chúng tôi sử dụng ngôn ngữ lập trình thanh lịch này cho khoa học dữ liệu, tích hợp quy trình làm việc, phát triển phần mềm, v.v ... Python cũng có khả năng phát triển GUI hoàn toàn.Python is fully capable of GUI development as well.

Người xây dựng GUI tốt nhất cho Python là gì?

10 Thư viện Python tốt nhất cho GUI..
Pyqt5.Được phát triển bởi Điện toán Riverbank, PYQT5 là một trong những khung Python phổ biến nhất cho GUI.....
Tkinter.Một thư viện Python hàng đầu khác cho GUI là Tkinter, đây là thư viện giao diện người dùng Python Python nguồn mở.....
Kivy.....
Wxpython.....
Pysimplegui.....
Libavg.....
Pyforms.....
PySide2..