Python:ncurses不打印传递给函数的值

时间:2015-11-14 19:28:10

标签: python user-interface ncurses curses

我正在为应用编写一个ncurses GUI。我在文件 public class BaseRepository<T> : IBaseRepository<T> where T : class { private IUnitOfWork unitOfWork; private DbSet<T> dbSet; public BaseRepository(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; this.dbSet = unitOfWork.DbContext.Set<T>(); } public virtual IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "") { IQueryable<T> query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query).ToList(); } else { return query.ToList(); } } public virtual IEnumerable<T> Get() { return dbSet.ToList(); } public virtual IEnumerable<T> Get(Expression<Func<T, bool>> predicate) { return dbSet.Where(predicate); } public virtual T Get(object id) { return dbSet.Find(id); } public virtual IQueryable<T> Where(Expression<Func<T, bool>> predicate) { return dbSet.Where(predicate); } public virtual IQueryable<T> Query() { return dbSet; } public bool Any(Expression<Func<T, bool>> predicate) { return dbSet.Any(predicate); } public T First(Expression<Func<T, bool>> predicate) { return dbSet.First(predicate); } public T FirstOrDefault(Expression<Func<T, bool>> predicate) { return dbSet.FirstOrDefault(predicate); } public virtual void Insert(T entity) { dbSet.Add(entity); } public virtual void Update(T entity) { if (unitOfWork.DbContext.Entry(entity).State == EntityState.Detached) { dbSet.Attach(entity); } unitOfWork.DbContext.Entry(entity).State = EntityState.Modified; } public virtual void Delete(object id) { T entityToDelete = dbSet.Find(id); Delete(entityToDelete); } public virtual void Delete(T entity) { if (unitOfWork.DbContext.Entry(entity).State == EntityState.Detached) { dbSet.Attach(entity); } dbSet.Remove(entity); } private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { unitOfWork.DbContext.Dispose(); } } this.disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public IEnumerable<T> ExecWithStoreProcedure(string query, params object[] parameters) { return unitOfWork.DbContext.Database.SqlQuery<T>(query, parameters); } } 中有以下代码:

admin.py

代码本身(函数#-*- coding: utf-8 -*- import curses.textpad from gui_helpers import * import global_vars as g def parse_command(win, command): # print_str(win, 0, 0, command) if command == "help": print_str(win, 1, 1, "Available commands: add - adds a board, list - lists boards, mkdir - makes board directory") def display_admin_screen(win, keypress): rows = win.getmaxyx()[0] cols = win.getmaxyx()[1] print_str_center(win, 0, 1, "Admin interface", g.RED|g.BOLD) print_str(win, 1, 0, "Command line") textpad_win = win.subwin(1, cols - 1, 3, 1) output_win = win.subwin(5, cols - 1, 5, 1) output_win.refresh() textpad_win.refresh() win.refresh() curses.curs_set(1) textpad = curses.textpad.Textbox(textpad_win) textpad.stripspaces = 0 textpad_win.move(0,0) textpad.edit() command = textpad.gather() parse_command(output_win, command) # print_str(output_win, 0, 0, command) textpad_win.clear() curses.curs_set(0) )在文件display_admin_screen()的循环中运行,该循环具有所有必需的库导入:

gui.py

我遇到的问题是,当我输入&#39; help&#39;在while True: keypress = stdscr.getch() display_title_bar(stdscr) if g.STATE == 'motd': display_motd(working_win, keypress) elif g.STATE == 'boards': boards_view.display_threads(working_win, g.SELECTED_BOARD, keypress) elif g.STATE == 'admin': admin.display_admin_screen(working_win, keypress) working_win.refresh() stdscr.refresh() 的文本框中,没有任何反应。它应该在admin.py中显示帮助文本。我已经检查过该值是否正确传递给函数output_win,并且它是一个正确的值。我认为这可能是在循环中创建子窗口的问题,所以我尝试在循环外的parse_command()中创建它们并将它们传递给函数,但无济于事。如果我只是告诉gui.py函数将某些内容写入输出窗口,那就没问题了。出于某种原因,parse_command()块似乎是一个问题。

2 个答案:

答案 0 :(得分:0)

问题是您的程序使用

从标准屏幕读取
stdscr.getch()

stdscr上刷新,可以覆盖任何其他窗口(例如output_win)。要解决此问题,您应该使用您希望 更新的窗口来调用.getch()

如果这样做很尴尬,可以使用.touchwin()wrefresh()强制给定窗口进行重新绘制。

答案 1 :(得分:0)

我终于发现了什么是错的。按Enter键后,命令附加了一堆空格(不是换行符\n)。我所要做的就是改变:

command = textpad.gather()

command = textpad.gather().strip()

它现在有效。