0

我正在尝试在 NPSAppManaged 应用程序的 FixedText 小部件中显示时间。

到目前为止,我有这个:

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()

我只是不确定如何.value从线程中引用小部件的参数并更新它。我应该做什么?

4

2 回答 2

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()
于 2022-01-14T01:12:42.750 回答
0

您需要将 分配npyscreen.FixedText给一个变量,如下所示:

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

现在您可以使用self.w_time.value它来访问它。

于 2017-03-24T15:55:17.017 回答