Hướng dẫn simple-term-menu python - python-menu đơn giản
Menu thiết bị đầu cuối đơn giảnTổng quanyay -S python-simple-term-menu4 Tạo các menu đơn giản cho các chương trình dòng lệnh tương tác. Nó có thể được sử dụng để cung cấp một sự lựa chọn các tùy chọn khác nhau cho người dùng. Các mục nhập menu có thể được chọn với các phím Mũi tên, J/K hoặc Emacs (C-N/C-P). Mô -đun sử dụng cơ sở dữ liệu đầu cuối để tự động phát hiện các tính năng thiết bị đầu cuối và vô hiệu hóa các kiểu không có sẵn. Hiện tại, Linux và MacOS được hỗ trợ. Show
Phá vỡ thay đổiTừ phiên bản 0.x đến 1.xNếu bạn cập nhật từ phiên bản 0.x đến 1.x, vui lòng xem xét các thay đổi phá vỡ sau:
Từ phiên bản 1.1 đến 1.2
Cài đặtyay -S python-simple-term-menu4 có sẵn trên PYPI cho Python 3.5+ và có thể được cài đặt với #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()6: python3 -m pip install simple-term-menu Nếu bạn sử dụng Arch Linux hoặc một trong những dẫn xuất của nó, bạn cũng có thể cài đặt yay -S python-simple-term-menu4 từ AUR: yay -S python-simple-term-menu Bạn cũng tìm thấy các tệp thực thi khép kín cho các bản phân phối Linux 64 bit và MacOS High Sierra và mới hơn trên trang phát hành. Chúng được tạo bằng pyinstaller và chỉ yêu cầu glibc> = 2.17 trên linux (sẽ ổn trên bất kỳ hệ thống Linux gần đây nào). Cách sử dụngTạo một menu với kiểu mặc địnhTạo một thể hiện của lớp yay -S python-simple-term-menu5 và chuyển các mục menu dưới dạng danh sách các chuỗi cho hàm tạo. Gọi phương thức #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()9 để xuất menu và đợi đầu vào bàn phím: #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main() Bạn sẽ nhận được một đầu ra như: Bây giờ bạn có thể chọn một mục nhập menu với các phím mũi tên hoặc ________ 30/________ 31 (chuyển động VIM) và chấp nhận sự lựa chọn của bạn bằng cách nhấn hoặc hủy menu bằng Escape, #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()2 hoặc #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()3. #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()9 Trả về chỉ mục nhập menu đã chọn hoặc #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()5 nếu menu bị hủy. Bạn có thể chuyển một #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()6 tùy chọn cho hàm tạo yay -S python-simple-term-menu5 sẽ được đặt phía trên menu. #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()6 có thể là một chuỗi đơn giản, một chuỗi đa dòng (với #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()9 Newlines) hoặc một danh sách các chuỗi. Điều tương tự cũng áp dụng cho tham số #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): terminal_menu = TerminalMenu(["entry 1", "entry 2", "entry 3"], accept_keys=("enter", "alt-d", "ctrl-i")) menu_entry_index = terminal_menu.show() print(terminal_menu.chosen_accept_key) if __name__ == "__main__": main()0, đặt một thanh trạng thái bên dưới menu. Ngoài ra, bạn có thể sử dụng tham số #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): terminal_menu = TerminalMenu(["entry 1", "entry 2", "entry 3"], accept_keys=("enter", "alt-d", "ctrl-i")) menu_entry_index = terminal_menu.show() print(terminal_menu.chosen_accept_key) if __name__ == "__main__": main()0 có thể gọi được lấy mục nhập hiện được chọn và trả về chuỗi thanh trạng thái. Kiểu dángBạn có thể chuyển các đối số kiểu dáng cho hàm tạo yay -S python-simple-term-menu5. Mỗi phong cách là một bộ của chuỗi từ khóa. Hiện tại các từ khóa sau được chấp nhận:
Bạn có thể thay đổi các kiểu sau:
Bằng cách đặt #!/usr/bin/env python3 import subprocess from simple_term_menu import TerminalMenu def list_tmux_sessions(): tmux_command_output = subprocess.check_output( ["tmux", "list-sessions", "-F#{session_id}:#{session_name}"], universal_newlines=True ) tmux_sessions = [] for line in tmux_command_output.split("\n"): line = line.strip() if not line: continue session_id, session_name = tuple(line.split(":")) tmux_sessions.append((session_name, session_id)) return tmux_sessions def main(): terminal_menu = TerminalMenu( ("|".join(session) for session in list_tmux_sessions()), preview_command="tmux capture-pane -e -p -t {}", preview_size=0.75, ) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()1, bạn có thể xác định một con trỏ khác hoặc vô hiệu hóa nó ( #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()5). Con trỏ mặc định là #!/usr/bin/env python3 import subprocess from simple_term_menu import TerminalMenu def list_tmux_sessions(): tmux_command_output = subprocess.check_output( ["tmux", "list-sessions", "-F#{session_id}:#{session_name}"], universal_newlines=True ) tmux_sessions = [] for line in tmux_command_output.split("\n"): line = line.strip() if not line: continue session_id, session_name = tuple(line.split(":")) tmux_sessions.append((session_name, session_id)) return tmux_sessions def main(): terminal_menu = TerminalMenu( ("|".join(session) for session in list_tmux_sessions()), preview_command="tmux capture-pane -e -p -t {}", preview_size=0.75, ) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()3. Tham số #!/usr/bin/env python3 import os from pygments import formatters, highlight, lexers from pygments.util import ClassNotFound from simple_term_menu import TerminalMenu def highlight_file(filepath): with open(filepath, "r") as f: file_content = f.read() try: lexer = lexers.get_lexer_for_filename(filepath, stripnl=False, stripall=False) except ClassNotFound: lexer = lexers.get_lexer_by_name("text", stripnl=False, stripall=False) formatter = formatters.TerminalFormatter(bg="dark") # dark or light highlighted_file_content = highlight(file_content, lexer, formatter) return highlighted_file_content def list_files(directory="."): return (file for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))) def main(): terminal_menu = TerminalMenu(list_files(), preview_command=highlight_file, preview_size=0.75) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()8 tùy chỉnh con trỏ đa chọn (mặc định là #!/usr/bin/env python3 import subprocess from simple_term_menu import TerminalMenu def list_tmux_sessions(): tmux_command_output = subprocess.check_output( ["tmux", "list-sessions", "-F#{session_id}:#{session_name}"], universal_newlines=True ) tmux_sessions = [] for line in tmux_command_output.split("\n"): line = line.strip() if not line: continue session_id, session_name = tuple(line.split(":")) tmux_sessions.append((session_name, session_id)) return tmux_sessions def main(): terminal_menu = TerminalMenu( ("|".join(session) for session in list_tmux_sessions()), preview_command="tmux capture-pane -e -p -t {}", preview_size=0.75, ) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()5). Đang tìm kiếm#!/usr/bin/env python3 import subprocess from simple_term_menu import TerminalMenu def list_tmux_sessions(): tmux_command_output = subprocess.check_output( ["tmux", "list-sessions", "-F#{session_id}:#{session_name}"], universal_newlines=True ) tmux_sessions = [] for line in tmux_command_output.split("\n"): line = line.strip() if not line: continue session_id, session_name = tuple(line.split(":")) tmux_sessions.append((session_name, session_id)) return tmux_sessions def main(): terminal_menu = TerminalMenu( ("|".join(session) for session in list_tmux_sessions()), preview_command="tmux capture-pane -e -p -t {}", preview_size=0.75, ) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()6 có tính năng tìm kiếm tích hợp để lọc các mục menu hiển thị. Khóa mặc định để kích hoạt chế độ tìm kiếm là #!/usr/bin/env python3 import subprocess from simple_term_menu import TerminalMenu def list_tmux_sessions(): tmux_command_output = subprocess.check_output( ["tmux", "list-sessions", "-F#{session_id}:#{session_name}"], universal_newlines=True ) tmux_sessions = [] for line in tmux_command_output.split("\n"): line = line.strip() if not line: continue session_id, session_name = tuple(line.split(":")) tmux_sessions.append((session_name, session_id)) return tmux_sessions def main(): terminal_menu = TerminalMenu( ("|".join(session) for session in list_tmux_sessions()), preview_command="tmux capture-pane -e -p -t {}", preview_size=0.75, ) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()7 (như trong VIM, ít hơn và các chương trình thiết bị đầu cuối khác). Nếu bạn thích khóa tìm kiếm khác, hãy chuyển tham số #!/usr/bin/env python3 import subprocess from simple_term_menu import TerminalMenu def list_tmux_sessions(): tmux_command_output = subprocess.check_output( ["tmux", "list-sessions", "-F#{session_id}:#{session_name}"], universal_newlines=True ) tmux_sessions = [] for line in tmux_command_output.split("\n"): line = line.strip() if not line: continue session_id, session_name = tuple(line.split(":")) tmux_sessions.append((session_name, session_id)) return tmux_sessions def main(): terminal_menu = TerminalMenu( ("|".join(session) for session in list_tmux_sessions()), preview_command="tmux capture-pane -e -p -t {}", preview_size=0.75, ) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()8 cho hàm tạo yay -S python-simple-term-menu5. #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()5 có thể được truyền để kích hoạt tìm kiếm trên mỗi khóa chữ cái. Rõ ràng, #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()0 và #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()1 không thể được sử dụng cho các chuyển động con trỏ trong chế độ đó. Sử dụng from simple_term_menu import TerminalMenu def main(): # Or use `None` instead of `""`: options = ["entry 1", "entry 2", "", "add", "edit"] # ["entry 1", "entry 2", None, "add", "edit"] terminal_menu = TerminalMenu(options, skip_empty_entries=True) # TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()3 và from simple_term_menu import TerminalMenu def main(): # Or use `None` instead of `""`: options = ["entry 1", "entry 2", "", "add", "edit"] # ["entry 1", "entry 2", None, "add", "edit"] terminal_menu = TerminalMenu(options, skip_empty_entries=True) # TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()4 thay thế. Chế độ tìm kiếm hỗ trợ cú pháp Python Regex. Truy cập tài liệu Python Re để biết thêm chi tiết. Các phần chuỗi của các mục menu khớp với mẫu tìm kiếm đã cho được tô sáng. Sử dụng tham số #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def list_files(directory="."): return (file for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))) def main(): terminal_menu = TerminalMenu(list_files(), preview_command="bat --color=always {}", preview_size=0.75) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()7 để điều chỉnh kiểu nổi bật theo ý thích của bạn. Theo mặc định, tìm kiếm là trường hợp không nhạy cảm. Đặt from simple_term_menu import TerminalMenu def main(): # Or use `None` instead of `""`: options = ["entry 1", "entry 2", "", "add", "edit"] # ["entry 1", "entry 2", None, "add", "edit"] terminal_menu = TerminalMenu(options, skip_empty_entries=True) # TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()6 thành from simple_term_menu import TerminalMenu def main(): # Or use `None` instead of `""`: options = ["entry 1", "entry 2", "", "add", "edit"] # ["entry 1", "entry 2", None, "add", "edit"] terminal_menu = TerminalMenu(options, skip_empty_entries=True) # TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()7 Nếu bạn thích dòng tìm kiếm nhạy cảm trường hợp. Chuyển from simple_term_menu import TerminalMenu def main(): # Or use `None` instead of `""`: options = ["entry 1", "entry 2", "", "add", "edit"] # ["entry 1", "entry 2", None, "add", "edit"] terminal_menu = TerminalMenu(options, skip_empty_entries=True) # TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()8 cho hàm tạo yay -S python-simple-term-menu5 để kích hoạt gợi ý tìm kiếm trong dòng tìm kiếm (như yay -S python-simple-term-menu00). Phím tắtBạn có thể xác định các phím tắt cho các mục menu đã chọn bằng cách chi tiêu một ký tự duy nhất được đặt trong ngoặc vuông (như yay -S python-simple-term-menu01). Vượt qua #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def list_files(directory="."): return (file for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))) def main(): terminal_menu = TerminalMenu(list_files(), preview_command="bat --color=always {}", preview_size=0.75) menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()9 và/hoặc #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()3 cho hàm tạo yay -S python-simple-term-menu5 để thay đổi kiểu nổi bật mặc định của các phím tắt. Theo mặc định, phương thức #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()9 trả về khi nhấn phím tắt. Nếu bạn chỉ muốn lựa chọn để nhảy mục tiêu phím tắt, hãy chuyển yay -S python-simple-term-menu06 cho hàm tạo yay -S python-simple-term-menu5. Nếu bạn đã cấu hình tìm kiếm sẽ được kích hoạt trên mỗi khóa chữ cái, tính năng phím tắt sẽ bị tắt. Chuyển yay -S python-simple-term-menu08 cho hàm tạo yay -S python-simple-term-menu5 để hiển thị các gợi ý phím tắt trong thanh trạng thái (hữu ích cho các menu rất dài cần cuộn). Ngoài ra, hãy vượt qua yay -S python-simple-term-menu10 Nếu bạn thích gợi ý tắt trong tiêu đề menu. Ví dụ phím tắtTạo một menu của một số loại trái cây và sử dụng chữ cái đầu tiên làm phím tắt: #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main() Các phím chấp nhận tùy chỉnhKhóa mặc định để chấp nhận một mục menu là yay -S python-simple-term-menu11. Vượt qua tham số yay -S python-simple-term-menu12 với một bộ khóa (dưới dạng chuỗi) cho hàm tạo yay -S python-simple-term-menu5 để sử dụng một bộ khóa chấp nhận khác. Các khóa chấp nhận tùy chỉnh có thể là các chữ cái ASCII đơn giản hoặc các chữ cái ASCII có khóa sửa đổi (Prepend yay -S python-simple-term-menu14 hoặc yay -S python-simple-term-menu15 cho chữ cái ASCII). Sử dụng thuộc tính yay -S python-simple-term-menu16 của phiên bản yay -S python-simple-term-menu5 để truy vấn chấp nhận khóa được người dùng nhấn. Xin lưu ý rằng không phải tất cả các kết hợp của các khóa sửa đổi và ASCII sẽ hoạt động tùy thuộc vào trình giả lập thiết bị đầu cuối và giao diện người dùng đồ họa của bạn. Ngoài ra, một số kết hợp tạo ra các khóa hiện có khác (ví dụ: yay -S python-simple-term-menu18 là yay -S python-simple-term-menu11 / trả lại vận chuyển). Ví dụ khóa chấp nhận tùy chỉnh#!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): terminal_menu = TerminalMenu(["entry 1", "entry 2", "entry 3"], accept_keys=("enter", "alt-d", "ctrl-i")) menu_entry_index = terminal_menu.show() print(terminal_menu.chosen_accept_key) if __name__ == "__main__": main() Multi-selectChuyển yay -S python-simple-term-menu20 cho hàm tạo yay -S python-simple-term-menu5 để kích hoạt chế độ đa chọn. Nhấn #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()0 hoặc #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()1 trên một mục menu tùy ý để thêm nó vào lựa chọn của bạn. Nhấn yay -S python-simple-term-menu11 (hoặc bất kỳ yay -S python-simple-term-menu25 được định cấu hình nào khác) để thêm mục nhập hiện được chọn làm mục cuối cùng vào lựa chọn và trả về phương thức #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()9 như bình thường. Trong chế độ đa chọn, phương thức #!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): options = ["entry 1", "entry 2", "entry 3"] terminal_menu = TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main()9 trả về một bộ xử lý được sắp xếp của tất cả các chỉ số menu đã chọn của bạn thay vì một int. Sử dụng thuộc tính yay -S python-simple-term-menu28 để có được một bộ của các chuỗi nhập menu thay thế. Bằng cách cài đặt yay -S python-simple-term-menu9, bạn có thể xác định một bộ khóa khác để chuyển đổi một mục đã chọn. Bằng cách vượt qua yay -S python-simple-term-menu30 Một gợi ý chế độ đa chọn được hiển thị trong thanh trạng thái. Nếu bạn không muốn yay -S python-simple-term-menu25 cũng chọn mục được tô sáng cuối cùng, bạn có thể vượt qua yay -S python-simple-term-menu32. Nếu không có mục menu nào được chọn rõ ràng, mục menu được tô sáng cuối cùng vẫn sẽ được thêm vào lựa chọn trừ khi bạn cũng vượt qua yay -S python-simple-term-menu33. Một danh sách tùy chọn (hoặc bất kỳ đối tượng có thể lặp lại nào khác) yay -S python-simple-term-menu34 cũng có thể được truyền để có các mục đã được chọn khi menu được hiển thị. Danh sách này có thể bao gồm các số nguyên đại diện cho các chỉ mục của danh sách yay -S python-simple-term-menu35 hoặc các chuỗi phù hợp với các yếu tố của yay -S python-simple-term-menu35. Số nguyên và chuỗi có thể được trộn lẫn. Ví dụ đa chọn#!/usr/bin/env python3 from simple_term_menu import TerminalMenu def main(): terminal_menu = TerminalMenu( ["dog", "cat", "mouse", "squirrel"], multi_select=True, show_multi_select_hint=True, ) menu_entry_indices = terminal_menu.show() print(menu_entry_indices) print(terminal_menu.chosen_menu_entries) if __name__ == "__main__": main() Xem trước cửa sổyay -S python-simple-term-menu4 có thể hiển thị bản xem trước cho mỗi lần nhập menu. Chuyển yay -S python-simple-term-menu38 cho hàm tạo yay -S python-simple-term-menu5 để kích hoạt tính năng tùy chọn này. yay -S python-simple-term-menu38 hoặc lấy một chuỗi lệnh sẽ được thực thi dưới dạng quy trình con hoặc một python có thể gọi được chuyển đổi một chuỗi mục nhập menu đã cho thành đầu ra xem trước. Nếu một chuỗi lệnh được đưa ra, mẫu yay -S python-simple-term-menu41 được thay thế bằng chuỗi mục nhập menu hiện tại. Nếu một mục menu có một thành phần dữ liệu bổ sung (được phân tách bằng yay -S python-simple-term-menu42), thay vào đó, nó sẽ được chuyển sang lệnh xem trước. yay -S python-simple-term-menu43 có thể được sử dụng cho một yay -S python-simple-term-menu42 theo nghĩa đen. Nếu bạn chỉ cần nối thêm một yay -S python-simple-term-menu42 (không có thành phần dữ liệu), cửa sổ xem trước sẽ bị tắt cho mục này. Đối số từ khóa bổ sung yay -S python-simple-term-menu46 có thể được sử dụng để kiểm soát chiều cao của cửa sổ xem trước. Nó được đưa ra dưới dạng một phần của chiều cao thiết bị đầu cuối hoàn chỉnh (mặc định: yay -S python-simple-term-menu47). Chiều rộng không thể được đặt, nó luôn luôn là chiều rộng hoàn chỉnh của cửa sổ đầu cuối. Vượt qua yay -S python-simple-term-menu48 với một chuỗi bạn chọn để tùy chỉnh tiêu đề cửa sổ xem trước (mặc định: yay -S python-simple-term-menu49) hoặc yay -S python-simple-term-menu50 để hủy kích hoạt đường viền xung quanh cửa sổ xem trước (cũng hủy kích hoạt chuỗi tiêu đề). Các lệnh xem trước được phép tạo mã màu ANSI Escape. Xem trước ví dụ
Bỏ qua các mục trốngSử dụng tham số Constructor yay -S python-simple-term-menu53 hoặc cờ yay -S python-simple-term-menu54 để diễn giải một giá trị chuỗi trống trong các mục menu dưới dạng mục menu trống (sẽ được bỏ qua khi lặp lại trên các mục). Giá trị #!/usr/bin/env python3 import os from simple_term_menu import TerminalMenu def main(): fruits = ["[a] apple", "[b] banana", "[o] orange"] terminal_menu = TerminalMenu(fruits, title="Fruits") menu_entry_index = terminal_menu.show() if __name__ == "__main__": main()5 luôn được coi là một mục menu trống độc lập với tham số yay -S python-simple-term-menu53. from simple_term_menu import TerminalMenu def main(): # Or use `None` instead of `""`: options = ["entry 1", "entry 2", "", "add", "edit"] # ["entry 1", "entry 2", None, "add", "edit"] terminal_menu = TerminalMenu(options, skip_empty_entries=True) # TerminalMenu(options) menu_entry_index = terminal_menu.show() print(f"You have selected {options[menu_entry_index]}!") if __name__ == "__main__": main() Sửa đổi bản địa hóa / văn bảnSử dụng các tham số hàm tạo
Để sửa đổi các văn bản tương ứng. Sử dụng trình giữ chỗ yay -S python-simple-term-menu59 cho khóa tìm kiếm trong yay -S python-simple-term-menu57 và cả yay -S python-simple-term-menu61 và yay -S python-simple-term-menu62 trong yay -S python-simple-term-menu58 nếu thích hợp. Cài đặt thêmHơn nữa, hàm tạo yay -S python-simple-term-menu5 lấy các tham số bổ sung này để thay đổi hành vi menu:
Chương trình dòng lệnhyay -S python-simple-term-menu4 có thể được sử dụng như một chương trình thiết bị đầu cuối trong các tập lệnh shell. Mã thoát của tập lệnh là chỉ mục dựa trên 1 của mục menu đã chọn. Mã thoát 0 báo cáo hành động hủy bỏ. Các đối số dòng lệnh sau được hỗ trợ: yay -S python-simple-term-menu0 Ví dụ với tùy chọn xem trướcThay vì sử dụng API Python như trong các ví dụ trước, một menu tệp có xem trước yay -S python-simple-term-menu51 cũng có thể được tạo từ dòng lệnh: yay -S python-simple-term-menu1 Ví dụ nâng cao hơnMột ví dụ nâng cao hơn với các menu phụ (nhờ Pageauc): yay -S python-simple-term-menu2 Các dự án tương tự
Đóng gópVui lòng mở một vấn đề trên GitHub nếu bạn gặp lỗi hoặc bỏ lỡ các tính năng. Vui lòng xem xét để gửi một yêu cầu kéo nếu bạn có thể dành thời gian để tự khắc phục vấn đề. Dự án này sử dụng tiền cam kết để đảm bảo chất lượng mã và kiểu mã nhất quán. Chạy yay -S python-simple-term-menu3 Để cài đặt tất cả các lớp lót dưới dạng móc git trong bản sao yay -S python-simple-term-menu4 cục bộ của bạn. |