0

I'm trying to display the time in a FixedText widget in an NPSAppManaged application.

So far I've got this:

import npyscreen
import datetime
import threading
from time import sleep

class MainForm(npyscreen.Form):
    def create(self):
        self.add(npyscreen.FixedText, value = "Time")

    def afterEditing(self):
        self.parentApp.setNextForm(None)

    def set_value(self):
        return "Tom"

class TestApp(npyscreen.NPSAppManaged):

    def onStart(self):
        self.registerForm("MAIN", MainForm())

        thread_time = threading.Thread(target=self.update_time,args=())
        thread_time.daemon = True
        thread_time.start()

    def update_time(self):
       while True:
           # self.f.wStatus2.value = datetime.datetime.now().ctime()
           # self.f.wStatus2.display()
           sleep(1)

if __name__ == "__main__":
    App = TestApp()
    App.run()

I'm just not sure how to reference the .value parameter for the widget from the thread and update it. What should I be doing?

cjm2671
  • 18,348
  • 31
  • 102
  • 161

2 Answers2

0

You need to assign the npyscreen.FixedText to a variable as such:

 def create(self): 
     self.w_time = self.add(npyscreen.FixedText, value = "Time")

Now you can use self.w_time.value to access it.

Andria
  • 4,712
  • 2
  • 22
  • 38
0
import npyscreen
import datetime
import threading
from time import sleep

class MainForm(npyscreen.Form):
    def create(self):
        self.date = self.add(npyscreen.TitleText, value = str(datetime.datetime.now()), editable=False, name='Something')

    def afterEditing(self):
        self.parentApp.setNextForm(None)

    def set_value(self):
        return "Tom"

class TestApp(npyscreen.NPSAppManaged):

    def onStart(self):

        self.textual = MainForm()
        self.registerForm("MAIN", self.textual)

        thread_time = threading.Thread(target=self.update_time,args=())
        thread_time.daemon = True
        thread_time.start()

    def update_time(self):
        while True:
            self.textual.date.value = str(datetime.datetime.now())
            self.textual.display()
            sleep(1)

if __name__ == "__main__":
    App = TestApp()
    App.run()
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '22 at 09:11