Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
ctrl+l
Bàn luận
Hầu hết thời gian, trong khi làm việc với Shell/thiết bị đầu cuối tương tác Python [không phải là một bảng điều khiển], chúng tôi kết thúc với đầu ra lộn xộn và muốn xóa màn hình vì một số lý do. Trong một shell/thiết bị đầu cuối tương tác, chúng ta chỉ có thể sử dụng
Nhưng, điều gì sẽ xảy ra nếu chúng ta muốn xóa màn hình trong khi chạy tập lệnh Python? Thật không may, không có từ khóa hoặc chức năng/phương thức tích hợp để xóa màn hình. Vì vậy, chúng tôi làm điều đó một mình.
Xóa màn hình trong hệ điều hành Windows
Python3
Phương pháp 1: Màn hình rõ ràng trong Python bằng CLS
os.system[
import pyautogui
pyautogui.hotkey['command', 'l']
0 import pyautogui
pyautogui.hotkey['command', 'l']
1Bạn có thể chỉ đơn giản là CL CLS để xóa màn hình trong Windows.
import
os
from os import system” but with that, you have to change system[‘clear’] to os.system[‘clear’].
Python3
Ví dụ 2: Màn hình rõ ràng trong Python bằng cách sử dụng Clear
Bạn cũng chỉ có thể nhập khẩu OS OS OS thay vì từ hệ thống nhập hệ điều hành, nhưng với điều đó, bạn phải thay đổi hệ thống [’rõ ràng] thành hệ điều hành [‘ rõ ràng]. & NBSP;
import pyautogui
pyautogui.hotkey['command', 'l']
2 import pyautogui
pyautogui.hotkey['command', 'l']
3import
import pyautogui
pyautogui.hotkey['command', 'l']
5 import pyautogui
pyautogui.hotkey['command', 'l']
2 import pyautogui
pyautogui.hotkey['command', 'l']
7import
import pyautogui
pyautogui.hotkey['command', 'l']
9import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
0 import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
1import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
2[2799, 575]
6import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
8import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
2import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
3 import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
4import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
5import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
5 import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
7import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
8import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
4import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
5import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
6import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
7import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
8 import pyautogui
pyautogui.hotkey['command', 'l']
1from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
0from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
1 import pyautogui
pyautogui.hotkey['command', 'l']
1from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
3import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
9[2799, 575]
03025 [2799, 575]
2 import pyautogui
pyautogui.hotkey['command', 'l']
0 import pyautogui
pyautogui.hotkey['command', 'l']
1
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
[2799, 575]
[2799, 575]
import pyautogui
pyautogui.hotkey['command', 'l']
import pyautogui
pyautogui.hotkey['command', 'l']
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
9[2799, 575]
0import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
5 [2799, 575]
2import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
2 import pyautogui
pyautogui.hotkey['command', 'l']
1subprocess module. Python3
Ví dụ 3: Xóa màn hình trong Python bằng cách sử dụng cuộc gọi
Bạn cũng chỉ có thể nhập khẩu OS OS OS thay vì từ hệ thống nhập hệ điều hành, nhưng với điều đó, bạn phải thay đổi hệ thống [’rõ ràng] thành hệ điều hành [‘ rõ ràng]. & NBSP;
import pyautogui
pyautogui.hotkey['command', 'l']
2 import pyautogui
pyautogui.hotkey['command', 'l']
3import
import pyautogui
pyautogui.hotkey['command', 'l']
5 import pyautogui
pyautogui.hotkey['command', 'l']
2 import pyautogui
pyautogui.hotkey['command', 'l']
7import
import pyautogui
pyautogui.hotkey['command', 'l']
9import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
0 import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
1from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
0from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
1 import pyautogui
pyautogui.hotkey['command', 'l']
1from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
3import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
2import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
3 import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
4import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
5import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
5 import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
7import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
8
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
9[2799, 575]
03025 [2799, 575]
2 import pyautogui
pyautogui.hotkey['command', 'l']
0 import pyautogui
pyautogui.hotkey['command', 'l']
1Python3
Phương pháp 1: Màn hình rõ ràng trong Python bằng CLS
Bạn cũng chỉ có thể nhập khẩu OS OS OS thay vì từ hệ thống nhập hệ điều hành, nhưng với điều đó, bạn phải thay đổi hệ thống [’rõ ràng] thành hệ điều hành [‘ rõ ràng]. & NBSP;
import pyautogui
pyautogui.hotkey['command', 'l']
2 import pyautogui
pyautogui.hotkey['command', 'l']
3import
import pyautogui
pyautogui.hotkey['command', 'l']
5import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
4import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
5os.system[
9 import pyautogui
pyautogui.hotkey['command', 'l']
1import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
4import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
5 import pyautogui
pyautogui.hotkey['command', 'l']
03 import pyautogui
pyautogui.hotkey['command', 'l']
1import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
4import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
5 import pyautogui
pyautogui.hotkey['command', 'l']
07 import pyautogui
pyautogui.hotkey['command', 'l']
1import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
4import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
5 import pyautogui
pyautogui.hotkey['command', 'l']
11 import pyautogui
pyautogui.hotkey['command', 'l']
1import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
4import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
5 import pyautogui
pyautogui.hotkey['command', 'l']
15 import pyautogui
pyautogui.hotkey['command', 'l']
1from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
0 import pyautogui
pyautogui.hotkey['command', 'l']
18 import pyautogui
pyautogui.hotkey['command', 'l']
1os.system[
import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
2 import pyautogui
pyautogui.hotkey['command', 'l']
1Có cách nào để xóa bảng điều khiển "chạy" trong Pycharm không? Tôi muốn một mã xóa/ẩn tất cả các bản in [] đã thực hiện trước đó. Giống như nút "Clear_all", nhưng không cần phải nhấn thủ công.
Tôi đã đọc rằng có một cách để làm điều đó trong một thiết bị đầu cuối với hệ điều hành ["cls"], nhưng trong pycharm, nó chỉ thêm một hình vuông nhỏ mà không xóa bất cứ thứ gì.
Ngoài ra, tôi không muốn sử dụng in ["\ n" *100] vì tôi không muốn có thể cuộn lại và xem các bản in trước đó.
Đã hỏi ngày 2 tháng 12 năm 2014 lúc 3:16Dec 2, 2014 at 3:16
Simon Peloquinsimon PeloquinSimon Peloquin
2411 huy hiệu vàng2 huy hiệu bạc7 huy hiệu đồng1 gold badge2 silver badges7 bronze badges
6
Trong Pycharm:
23 [hoặc sở thích pycharm];Pycharm preferences];import pyautogui pyautogui.hotkey['command', 'l']
- Tìm kiếm: "Xóa tất cả";
- Nhấp đúp vào -> Thêm phím tắt bàn phím [đặt nó thành
24 hoặc bất cứ điều gì]import pyautogui pyautogui.hotkey['command', 'l']
- Thưởng thức khóa nóng mới này trong bảng điều khiển Pycharm của bạn!
Đã trả lời ngày 16 tháng 1 năm 2017 lúc 5:36Jan 16, 2017 at 5:36
2
Làm thế nào để
- Tải xuống gói này //github.com/asweigart/pyautogui. Nó cho phép Python gửi các nét chính.
Bạn có thể phải cài đặt một số gói khác trước
Nếu bạn đang cài đặt Pyautogui từ Pypi bằng PIP:
Windows không có sự phụ thuộc. Các phần mở rộng Win32 không cần phải cài đặt.
OS X cần cài đặt mô-đun PyOBJC-Core và PyOBJC [theo thứ tự đó].
Linux cần cài đặt mô-đun python3-xlib [hoặc python-xlib cho python 2]. Gối cần được cài đặt và trên Linux, bạn có thể cần cài đặt các thư viện bổ sung để đảm bảo PNG/JPEG của Gối hoạt động chính xác. Nhìn thấy:
- Đặt phím tắt để xóa cửa sổ chạy trong Pycharm như được giải thích bởi Taylan Aydinli
- CMD +, [hoặc sở thích pycharm];
- Tìm kiếm: "Xóa tất cả"; Nhấp đúp vào ->
- Thêm phím tắt bàn phím [đặt nó thành Ctrl + L hoặc bất cứ thứ gì]
- Thưởng thức khóa nóng mới này trong bảng điều khiển Pycharm của bạn!
Đã trả lời ngày 16 tháng 1 năm 2017 lúc 5:36
import pyautogui pyautogui.hotkey['command', 'l']
Làm thế nào để
Tải xuống gói này //github.com/asweigart/pyautogui. Nó cho phép Python gửi các nét chính.
Bạn có thể phải cài đặt một số gói khác trước
Nếu bạn đang cài đặt Pyautogui từ Pypi bằng PIP:
import pyautogui
from time import sleep
sleep[2]
print[pyautogui.position[]]
Windows không có sự phụ thuộc. Các phần mở rộng Win32 không cần phải cài đặt.
[2799, 575]
OS X cần cài đặt mô-đun PyOBJC-Core và PyOBJC [theo thứ tự đó].
import pyautogui
while True:
input_1 = input["?"]
print[input_1]
pyautogui.click[x=2799, y=575]
pyautogui.hotkey['command', 'l']
Linux cần cài đặt mô-đun python3-xlib [hoặc python-xlib cho python 2]. Gối cần được cài đặt và trên Linux, bạn có thể cần cài đặt các thư viện bổ sung để đảm bảo PNG/JPEG của Gối hoạt động chính xác. Nhìn thấy:♦
Đặt phím tắt để xóa cửa sổ chạy trong Pycharm như được giải thích bởi Taylan Aydinli21 gold badges77 silver badges129 bronze badges
CMD +, [hoặc sở thích pycharm];Nov 18, 2017 at 15:05
Jonathan de Wetjonathan de WetJonathan De Wet
3261 Huy hiệu vàng4 Huy hiệu bạc12 Huy hiệu đồng1 gold badge4 silver badges12 bronze badges
0
Phiên bản cộng đồng Pycharm 2020.1.3
Bạn có thể nhấp chuột phải bất cứ nơi nào phía trên dòng hiện tại trên bảng điều khiển và chọn tùy chọn "Xóa tất cả". Nó sẽ xóa bảng điều khiển
Baldr
2.79110 Huy hiệu vàng43 Huy hiệu bạc60 Huy hiệu Đồng10 gold badges43 silver badges60 bronze badges
Đã trả lời ngày 15 tháng 7 năm 2020 lúc 15:24Jul 15, 2020 at 15:24
1
Phương pháp dễ dàng: Phím tắt: Kiểm soát k, nhấp chuột phải vào thiết bị đầu cuối và bộ đệm rõ ràng
Đã trả lời ngày 4 tháng 12 năm 2019 lúc 10:04Dec 4, 2019 at 10:04
Ngoài ra còn có một cách khác để thực hiện nó bằng cách sử dụng lớp hệ thống từ hệ điều hành. Tất cả những gì bạn cần làm là có mã này:
from os import system, name
# define our clear function
def clear[]:
# for windows the name is 'nt'
if name == 'nt':
_ = system['cls']
# and for mac and linux, the os.name is 'posix'
else:
_ = system['clear']
# Then, whenever you want to clear the screen, just use this clear function as:
clear[]
Tuy nhiên, để chức năng này hoạt động trong pycharm, bạn cần bật "mô phỏng thiết bị đầu cuối trong bảng điều khiển đầu ra". Bạn có thể tìm thấy điều này trong cấu hình chỉnh sửa của tệp nơi bạn muốn sử dụng chức năng rõ ràng, sau đó nó thuộc tùy chọn thực thi. Đây là ảnh chụp màn hình: Pycharm Screensho
Đã trả lời ngày 15 tháng 5 năm 2021 lúc 4:21May 15, 2021 at 4:21
Bạn chỉ có thể thực hiện một ["\ n" * 100000000], vì vậy sẽ không thể cuộn lại.
Đã trả lời ngày 21 tháng 1 năm 2020 lúc 19:54Jan 21, 2020 at 19:54
Trong thiết bị đầu cuối pycharm, bạn có thể gõ 'CL' giống như trong thiết bị đầu cuối Linux.
Đối với bảng điều khiển Python [nơi bạn thấy đầu ra] gán một shortkey cho "xóa tất cả" trong tệp -> cài đặt -> keymap -> khác -> "xóa tất cả"
Bạn cũng có thể nhấp vào đâu đó trên Pythonconsole -> Nút bên phải -> Xóa.
Hy vọng nó giúp
Đã trả lời ngày 12 tháng 7 năm 2020 lúc 17:32Jul 12, 2020 at 17:32
Tôi chỉ nói rằng thay vì gặp rắc rối khi thiết lập một phím tắt, bạn chỉ có thể thiết lập một lệnh bằng pyautogui để nhấp vào thùng rác ở bên cạnh cửa sổ, ví dụ
Lưu ý, để cài đặt pyautogui, nhấp vào cuối dòng
import pyautogui
pyautogui.hotkey['command', 'l']
26, sau đó nhấn Alt+Enter và nhấp vào Cài đặt PYAUTOGUI.import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep[2] # hover your mouse over bin in this time
mousepos = pyautogui.position[] gets current pos of mouse
x,y = mousepos # storing mouse position
print[mousepos] # prints current pos of mouse
# then to clear it;
pyautogui.click[x, y] # and just put this line of code wherever you want to clear it
.
Đã trả lời ngày 19 tháng 11 năm 2020 lúc 7:05Nov 19, 2020 at 7:05
Chỉ cần nhấp vào biểu tượng thùng rác ở bên trái của cửa sổ lệnh và nó xóa lịch sử lệnh!
Đã trả lời ngày 26 tháng 10 năm 2019 lúc 17:18Oct 26, 2019 at 17:18
Sleharsleharslehar
3113 Huy hiệu bạc6 Huy hiệu đồng3 silver badges6 bronze badges
Trong Pycharm 2019.3.3, bạn có thể nhấp chuột phải và chọn nút "xóa tất cả". Điều này đang xóa tất cả dữ liệu bằng văn bản bên trong bảng điều khiển và thật không may, đây là thủ công.
Đã trả lời ngày 5 tháng 1 năm 2021 lúc 22:21Jan 5, 2021 at 22:21
Sinansinansinan
333 huy hiệu đồng3 bronze badges
Xin lỗi phải nói điều này, ở đây câu hỏi chính là làm thế nào để thực hiện theo chương trình trong khi mã của tôi đang chạy, tôi muốn mã của mình xóa dữ liệu trước đó và ở một giai đoạn nào đó và sau đó tiếp tục chạy mã. Nó sẽ hoạt động như nút đặt lại.
Sau khi dành thời gian cho nghiên cứu, tôi đã giải quyết vấn đề của mình bằng giải pháp của Mahak Khurmi //stackoverflow.com/a/67543234/16878188.
bad_coder
9.81620 Huy hiệu vàng40 Huy hiệu bạc64 Huy hiệu đồng20 gold badges40 silver badges64 bronze badges
Đã trả lời ngày 10 tháng 9 năm 2021 lúc 11:41Sep 10, 2021 at 11:41
2
Nếu bạn chỉnh sửa cấu hình chạy, bạn có thể kích hoạt "mô phỏng thiết bị đầu cuối trong bảng điều khiển đầu ra" và bạn có thể sử dụng dòng
import pyautogui
pyautogui.hotkey['command', 'l']
27 và nó sẽ hoạt động bình thường.
S.B
10,4K7 Huy hiệu vàng20 Huy hiệu bạc41 Huy hiệu đồng7 gold badges20 silver badges41 bronze badges
Đã trả lời ngày 15 tháng 5 lúc 17:44May 15 at 17:44
Iconman đã có câu trả lời dễ nhất.
Nhưng chỉ đơn giản là in "\ n" * 20 [hoặc bất kỳ chiều cao đầu cuối của bạn] sẽ xóa màn hình và sự khác biệt duy nhất là con trỏ nằm ở phía dưới.will clear the screen, and the only difference is that the cursor is at the bottom.
Tôi đến đây vì tôi muốn thấy trực quan mỗi bước của một quá trình phức tạp [tôi đang thực hiện một thanh tiến trình] và thiết bị đầu cuối đã có đầy đủ thông tin ghi nhật ký.
Cuối cùng tôi đã in ["A" * 40] * 20, và sau đó "B" và "C", v.v., và sau đó quay nó. Xem lại video giúp bạn dễ dàng thấy bao nhiêu giây mỗi bước. Vâng, tôi biết tôi có thể sử dụng dấu thời gian, nhưng điều này thật thú vị!
Đã trả lời ngày 28 tháng 10 lúc 9:51Oct 28 at 9:51