几周以来,我一直在尝试使用 npyscreen 创建应用程序,但我一直遇到挂断。我可以从我的主要形式切换到第二种形式。但是当我尝试切换回主窗体时,应用程序实际上什么也没做。我的函数被调用,该self.parentApp.switchForm()
函数被调用,但没有做任何事情。
预期的功能是让主表单允许输入名称。按回车后,输入处理程序验证它是否是有效输入(检查它是否存在于预定义的字典中)。如果是,它从字典中收集值,将该信息设置为将在下一个“SerialForm”显示的表单中,然后切换到该表单。然后用户在该表格中输入第一个序列号的信息。如果信息匹配,它将转到下一个输入字段。否则它保持在相同的输入上。一旦输入了“SerialForm”中的两个字段并匹配了它们应该匹配的内容,程序就会记录该信息并切换回“MAIN”表单。
self.parentApp.switchForm('MAIN')
在我调用self.serialTwoInputHandler()
调试之前一切正常,但在调用“SerialForm”之后仍然存在所有填充的值(根本没有任何变化。)没有出现异常或错误,我只是不知所措.
NPSApp托管
class Application(nps.NPSAppManaged):
def __init__(self, *args, lines=None, columns=None, **kwargs):
super(Application, self).__init__(*args, **kwargs)
def onStart(self):
# self.addFormClass('MAIN', TerminalNameForm, name='EMV Inventory')
self.addForm('MAIN', TerminalNameForm, name='EMV Inventory')
self.addForm('SerialForm', SerialNumbersForm, name='')
TerminalNamesForm(“主”表单)
class TerminalNameForm(nps.FormBaseNew):
def __init__(self, *args, lines=None, columns=None, **kwargs):
if(not lines or not columns):
# Get the full terminal size as defualt
from terminalsize import get_terminal_size
size = get_terminal_size()
if (not columns):
columns = size[0]
if (not lines):
lines = size[1]
super(TerminalNameForm, self).__init__(
*args, lines=lines, columns=columns, **kwargs)
def create(self):
self.promptLabel = self.add(FixedText, value='Enter Terminal Name:')
self.nameInput = self.add(TitleText, name='> ')
self.nameInput.setReturnHandler(self.nameInputHandler)
def nameInputHandler(self, *args, **keywords):
if(not self.nameInput.value):
return
inputName = self.nameInput.value
global terminalNames
if(inputName not in terminalNames):
self.promptLabel.color = 'DANGER'
self.promptLabel.value = f'Invalid Name: {inputName}'
self.promptLabel.display()
return
sn1 = terminalNames[inputName]['serial1']
sn2 = terminalNames[inputName]['serial2']
self.parentApp.getForm('SerialForm').setExpectedValues(inputName, sn1, sn2)
self.parentApp.switchForm('SerialForm')
def quit(self):
self.parentApp.switchForm(None)
SerialNumbersForm('SerialForm' 表格)
class SerialNumbersForm(nps.FormBaseNew):
#def __init__(self, name, sn1, sn2, *args, lines=None, columns=None, **kwargs):
def __init__(self, *args, lines=None, columns=None, **kwargs):
if(not lines or not columns):
# Get the full terminal size as defualt
from terminalsize import get_terminal_size
size = get_terminal_size()
if (not columns):
columns = size[0]
if (not lines):
lines = size[1]
self.terminalName = ''
self.expectedSerialOne = ''
self.expectedSerialTwo = ''
super(SerialNumbersForm, self).__init__(*args, lines=lines, columns=columns, **kwargs)
def create(self):
self.terminalNameLabel = self.add(FixedText, value=f'Name: {self.terminalName}')
self.expectedLabel = self.add(FixedText, value='Expected Values:', color='NO_EDIT')
self.expectedSerialOneLabel = self.add(TitleFixedText, name='\tS/N 1:', value=self.expectedSerialOne)
self.expectedSerialTwoLabel = self.add(TitleFixedText, name='\tS/N 2:', value=self.expectedSerialTwo)
newPos = self.expectedSerialTwoLabel.rely+2
self.serialOneInput = self.add(TitleText, name='S/N 1:', rely=newPos)
self.serialTwoInput = self.add(TitleText, name='S/N 2:', hidden=True)
self.add_handlers({'^C': quit})
self.serialOneInput.setReturnHandler(self.serialOneInputHandler)
self.serialTwoInput.setReturnHandler(self.serialTwoInputHandler)
def serialOneInputHandler(self, *args, **keywords):
if(not self.serialOneInput.value):
return
inputValue = self.serialOneInput.value
if(inputValue == self.expectedSerialOne):
self.serialOneInput.set_editing(False)
self.serialTwoInput.set_editing(True)
else:
# WHY. WONT. EDITING. COLOR. CHANGE!
self.serialOneInput.color = 'DANGER'
self.serialOneInput.labelColor = 'DANGER'
self.serialOneInput.label_widget.color = 'DANGER'
self.serialOneInput.label_widget.highlight_color = 'DANGER'
self.serialOneInput.entry_widget.color = 'DANGER'
self.serialOneInput.entry_widget.highlight_color = 'DANGER'
self.serialOneInput.display()
print()
def serialTwoInputHandler(self, *args, **keywords):
if(not self.serialTwoInput.value):
return
inputValue = self.serialTwoInput.value
if(inputValue == self.expectedSerialTwo):
self.parentApp.setNextForm('MAIN')
self.parentApp.switchFormNow()
def setExpectedValues(self, name, sn1, sn2):
self.terminalName = name
self.terminalNameLabel.value = f'Name: {name}'
self.expectedSerialOne = sn1
self.expectedSerialOneLabel.value = sn1
self.expectedSerialTwo = sn2
self.expectedSerialTwoLabel.value = sn2
return
有一些函数调用你不会认出我已经添加到覆盖的小部件类中。但是通过直接调用该方法不会影响表单切换功能。
例如,我已经覆盖了每个文本输入小部件以包括以下内容:
def setReturnHandler(self, function):
handlers = {curses.KEY_ENTER: function, # By key identity
curses.ascii.NL: function, # Unix character
curses.ascii.CR: function # NT character
}
self.add_handlers(handlers)
def hide(self, state):
self.hidden = state
self.display()
return
def show(self, state):
self.hidden = not state
self.display()
return
def set_editable(self, editable):
super(TitleText, self).set_editable(editable)
self.editable = editable
self.entry_widget.editable = editable
if(editable):
self.hidden = False
return
def set_editing(self, editing):
if(editing):
self.set_editable(True)
self.editing = editing # Affects the widget title color
self.entry_widget.editing = editing
self.display()
if not editing:
self.exit()
else:
self.edit()
return
def exit(self):
self.entry_widget.how_exited = 1
return