小编Cla*_*rts的帖子

如何组合多个TUI表单来编写更复杂的应用程序?

我想编写一个带有基于T ext的U ser I接口(TUI)的程序,它由几种形式组成.

介绍几种形式.

  • 第一个表单包含"列表".每个列表元素代表一个按钮.
  • 如果按下相应的按钮,则应出现另一个表单,其中可以输入列表条目的数据.
  • 然后再次显示第一个表单(使用更新的列表条目).

这是我的尝试,它使用库npyscreen但不返回第一个表单.代码也不包含更改列表项的逻辑.

#! /usr/bin/env python3
# coding:utf8

import npyscreen

# content
headers = ["column 1", "column 2", "column 3", "column 4"]
entries = [["a1", "a2", "a3", "a4"],
           ["b1", "b2", "b3", "b4"],
           ["c1", "c2", "c3", "c4"],
           ["d1", "d2", "d3", "d4"], 
           ["e1", "e2", "e3", "e4"]]


# returns a string in which the segments are padded with spaces.
def format_entry(entry):
    return "{:10} | {:10} | {:10} | {:10}".format(entry[0], entry[1] , …
Run Code Online (Sandbox Code Playgroud)

python tui console-application urwid npyscreen

5
推荐指数
1
解决办法
624
查看次数

Python + MongoDB,如何动态选择DB Collection

如何使用 Python 动态指向 MongoDB 中的特定集合名称?

我从网络中的几十个传感器中的任意一个接收数据,并且我想将原始数据存储到以传感器命名的数据库集合中。

# Server Parameters
host = '1.2.3.4'
port = 27017
client = MongoClient(host, port)
db = client.myDB                # use database myDB

# receive data from sensors
# {"sensorName":"...", "x":"...", "y":"...", "z":"...", "time":"..."}

db.SensorA.insert_one({...})    # record raw data in the collection for SensorA
db.SensorB.insert_one({...})
db.SensorC.insert_one({...})
Run Code Online (Sandbox Code Playgroud)

我不想明确地写db.SensorName.insert_one({...}),而是想以某种方式引用给定的传感器/集合名称。

谢谢

python reflection collections introspection mongodb

3
推荐指数
1
解决办法
1646
查看次数

使用 npyscreen 在表单之间传递值

我正在尝试npyscreen在 python 中创建一个简单的curses应用程序,该应用程序请求用户在一个屏幕上输入,然后在另一个屏幕上为用户验证它。

大多数情况下,这是为了理解如何在 内部存储和检索值npyscreen。我确信我错过了一些简单的东西,但我无法在文档中找到(或理解?)答案。

下面的示例代码将无法正确传递值:

#!/usr/bin/env python3.5

import npyscreen as np

class EmployeeForm(np.Form):
    def afterEditing(self):
        self.parentApp.switchForm('CONFIRMFM')

    def create(self):
        self.myName = self.add(np.TitleText, name='Name')
        self.myDepartment = self.add(np.TitleSelectOne, scroll_exit=True, max_height=3, name='Department', values = ['Department 1', 'Department 2', 'Department 3'])
        self.myDate = self.add(np.TitleDateCombo, name='Date Employed')

class EmployeeConfirmForm(np.Form):
    def afterEditing(self):
        self.parentApp.setNextForm(None)

    def create(self):
        self.value = None
        self.wgName   = self.add(np.TitleText, name = "Name:",)
        self.wgDept = self.add(np.TitleText, name = "Dept:")
        self.wgEmp      = self.add(np.TitleText, name = "Employed:")

    def beforeEditing(self):
        if self.value:
            self.name = …
Run Code Online (Sandbox Code Playgroud)

python-curses

2
推荐指数
1
解决办法
3456
查看次数

如何使用“print”函数清除控制台行

前提

我试图基本上清除控制台行,但不是在不使用空格的情况下清除整个控制台窗口,这样我就不会从上次打印的内容中获得额外的字符。例如:

# This causes characters from the last thing printed:
print("I don't know you.", end="\r")
print("Hello Jim!", end="\r")

# Yields the following (without the quotations) -->
# "Hello Jim!ow you."
Run Code Online (Sandbox Code Playgroud)

现在要解决这个问题可以这样做:

# This causes characters from the last thing printed:
print("I don't know you.", end="\r")
print("Hello Jim!", end="\r")

# Yields the following (without the quotations) -->
# "Hello Jim!ow you."
Run Code Online (Sandbox Code Playgroud)

问题

我如何

  1. 只是打印出来"Hello Jim!"而不是"Hello Jim! "(两者显然都没有引号)
  2. 清除该行
  3. 虽然没有清除整个控制台(这样我就可以输出除最后一行之外的其他内容)

具体来说,当改变尺寸时(例如控制台宽度从 17 到 30),控制台中会发生类似的情况,在我的情况下,这种情况经常发生:

Hello Jim! …
Run Code Online (Sandbox Code Playgroud)

python console urwid

1
推荐指数
1
解决办法
1127
查看次数