我想做一個有兩個窗口的終端應用程序。第一個窗口應該讀取用戶鍵入的命令(就像在vim中鍵入:一樣),而第二個窗口應該顯示定期更新的內容(例如:計時器)
為了使顯示窗口和命令窗口分別工作,我使用多線程策略。它一開始就有用,但很快當我寫了兩封以上的信時,顯示器就壞了。(右上面板)。
pycharm打開出現兩個窗口?window.getstr部分似乎仍然正常工作,因為當我鍵入quit+時程序將終止
顯示器出問題了,我想是和移動光標或其他東西有關。有人能指出如何正確地做這件事嗎?或者,有沒有關于使用python和curses創建類似vim的接口的文章?
代碼如下:import sys, os, json, time, datetime, math, curses, thread
python如何彈出輸入窗口,COUNTER = 0
def my_raw_input(window, r, c, prompt_string):
curses.echo()
python控制windows窗口,window.addstr(r, c, prompt_string)
window.refresh()
input = window.getstr(r + 1, c)
并輸入數據、return input
def count(window):
global COUNTER
while True:
window.addstr(3, 0, '%d'%(COUNTER))
if COUNTER >= 1000:
COUNTER = 0
COUNTER += 1
window.refresh()
def main(args):
# create stdscr
stdscr = curses.initscr()
stdscr.clear()
# allow echo, set colors
curses.echo()
curses.start_color()
curses.use_default_colors()
# define 2 windows
command_window = curses.newwin(3, 30, 0, 0)
display_window = curses.newwin(6, 30, 5, 0)
command_window.border()
display_window.border()
# thread to refresh display_window
thread.start_new_thread(count, (display_window,))
# main thread, waiting for user's command.
while True:
command = my_raw_input(command_window, 0, 0, 'Enter your command :')
if command == 'quit':
break
else:
command_window.addstr(1, 0, ' '*len(command))
curses.endwin()
curses.wrapper(main)
編輯:與建議的“重復問題”不同,我需要獲取字符串,而不是只檢測按鍵。
謝謝你。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态