我目前的临时方法是记录到文本文件,但这不是很互动。我试过使用pdb,但这似乎与 urwid 不符,pdb一旦遇到断点就不会接受任何输入。
我正在尝试制作一个简单的 urwid 作为无限循环的输出屏幕。它需要输出来自另一个类的数据。
我现在找到的解决方案是:有一个带有队列属性的 Printer 类(实际输出类的测试替换器)。当它需要显示某些内容时,会将其附加到队列中。然后,有一个 Interface 类(实际的接口)及其自己的 Printer 实例。与 MainLoop 并行运行的线程检查队列是否有项目,如果有,则输出它们。由于 Printer 的 main 函数是一个无限循环,它也有自己的线程 - 在这个测试中,它只是每隔几秒输出“Hello”。
这是代码:
import urwid
import threading
import time
class Interface:
palette = [
('body', 'white', 'black'),
('ext', 'white', 'dark blue'),
('ext_hi', 'light cyan', 'dark blue', 'bold'),
]
header_text = [
('ext_hi', 'ESC'), ':quit ',
('ext_hi', 'UP'), ',', ('ext_hi', 'DOWN'), ':scroll',
]
def __init__(self):
self.header = urwid.AttrWrap(urwid.Text(self.header_text), 'ext')
self.flowWalker = urwid.SimpleListWalker([])
self.body = urwid.ListBox(self.flowWalker)
self.footer = urwid.AttrWrap(urwid.Edit("Edit: "), 'ext')
self.view …Run Code Online (Sandbox Code Playgroud) 我认为代码比用文字可以更好地解释问题。这是my_abc.py中的代码:
from abc import ABCMeta, abstractmethod
class MyABC(object):
__metaclass__ = ABCMeta
@abstractmethod
def print(self):
pass
Run Code Online (Sandbox Code Playgroud)
这是my_class.py中的代码
from my_abc import MyABC
from third_party_package import SomeClass
class MyClass(MyABC, SomeClass):
def __init__(self):
super(MyClass, self).__init__()
def print(self):
print('Hello ABC')
Run Code Online (Sandbox Code Playgroud)
当我尝试运行my_class.py时,我得到:
TypeError:调用元类基础元类冲突时出错:派生类的元类必须是其所有基础元类的(非严格)子类
我知道我可以创建一个直接从我的接口MyABC继承的类,然后创建另一个类,然后再从我创建的该类和第三方模块类继承。
我的问题是:是否有另一种更好,更适当的方法直接执行此操作,而无需为我的目的创建中间类?
AttributeError: 'BigText' object has no attribute 'rows'当尝试在列表框顶部放置 BigText 时,我不断收到错误。我知道 BigText 是一个“固定”小部件,而 ListBox 需要一个“流”小部件,但无论我如何尝试,我似乎都无法让我的程序获取 BigText。这是我尝试过的详尽示例:
head_title = urwid.BigText(('banner', u'Header'), urwid.HalfBlock5x4Font())
head = urwid.Filler(head_title)
# head = urwid.AttrMap(head, 'banner')
# head = urwid.AttrMap(head, 'streak')
head = urwid.BoxAdapter(head, 3)
print head
# this gives me `<BoxAdapter flow widget <Filler box widget <BigText fixed widget>> height=3>`
body = [head, urwid.Divider()]
return urwid.ListBox(body)
Run Code Online (Sandbox Code Playgroud)
谢谢!
一般问题:完全像父类一样初始化子类,但添加单个属性的最简单/“最pythonic”方法是什么?
我的具体问题:我想扩展(Urwid)Edit对象以包括单个附加属性my_attribute;我已经将原始签名复制到__init__和中super().__init__,但是签名中有一些未定义的参数/常量(LEFT,SPACE),我不明白在父类中如何设置它们。以下是我的(中断)类定义和父init方法:
class MyEdit(urwid.Edit):
def __init__(self, my_attribute, caption="", edit_text="", multiline=False, align=LEFT, wrap=SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None):
super().__init__(caption="", edit_text="", multiline=False, align=LEFT, wrap=SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None)
self.my_attribute = []
# super().__super.__init__("", align, wrap, layout)
def my_method(self):
#some code that modifies my_attribute
return self.my_attribute
class Edit(Text):
"""
Text editing widget implements cursor movement, text insertion and
deletion. A caption may prefix the editing area. Uses text class …Run Code Online (Sandbox Code Playgroud) 是否可以跟踪urwid.ListBox对象中突出显示项目的更改?甚至通过一个ListWalker物体?
我想,当用户使用箭头键从一个项目移动到另一个调用回调[],[],不是当用户点击[Enter]一个项目.
我可以使用gevent和zeromq实现聊天守护程序,但我想为其创建一个控制台UI。
我与ncurses的第一次尝试失败了,所以我尝试Urwid,发现该项目寿司正想正是我想要的:

我研究了源代码,但是由于不熟悉控制台UI编程,因此未能找出产生此结果的部分(特别是因为它使用了多个选项卡),而我想我必须使用connect信号。
是否有人可以做到这一点?
我不需要整个程序,我可以自己处理通信,守护程序,选项以及所有其他内容。
只是设置UI的基础知识,在底部输入一些文本,然后异步通知顶部面板在不阻止底部输出的情况下添加一些文本。
谢谢
我是Python和urwid的新手,我在这里尝试了教程示例http://excess.org/urwid/docs/tutorial/
然而,虽然第一个工作正常,但大多数后来的工作似乎不起作用.例如,尝试运行这个特定的:
import urwid
choices = u'Chapman Cleese Gilliam Idle Jones Palin'.split()
def menu(title, choices):
body = [urwid.Text(title), urwid.Divider()]
for c in choices:
button = urwid.Button(c)
urwid.connect_signal(button, 'click', item_chosen, c)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
return urwid.ListBox(urwid.SimpleFocusListWalker(body))
def item_chosen(button, choice):
response = urwid.Text([u'You chose ', choice, u'\n'])
done = urwid.Button(u'Ok')
urwid.connect_signal(done, 'click', exit_program)
main.original_widget = urwid.Filler(urwid.Pile([response,
urwid.AttrMap(done, None, focus_map='reversed')]))
def exit_program(button):
raise urwid.ExitMainLoop()
main = urwid.Padding(menu(u'Pythons', choices), left=2, right=2)
top = urwid.Overlay(main, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
align='center', width=('relative', 60),
valign='middle', height=('relative', 60),
min_width=20, …Run Code Online (Sandbox Code Playgroud) 我是 urwid 的新手,认为我误解了一些东西。我不明白为什么这不起作用。而且我不明白错误消息。
#!/usr/bin/env python3
import urwid
def show_or_exit(key):
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
txt.set_text(repr(key))
txt = urwid.Text('FooBar')
fil = urwid.Filler(txt, valign='middle', height=('relative', 70))
box = urwid.LineBox(fil)
pad = urwid.Padding(box, align='center', width=('relative', 85))
loop = urwid.MainLoop(pad, unhandled_input=show_or_exit)
loop.run()
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)
现在要解决这个问题可以这样做:
# 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)
我如何
"Hello Jim!"而不是"Hello Jim! "(两者显然都没有引号)具体来说,当改变尺寸时(例如控制台宽度从 17 到 30),控制台中会发生类似的情况,在我的情况下,这种情况经常发生:
Hello Jim! …Run Code Online (Sandbox Code Playgroud) 我正在使用urwid提供的wicd应用程序查看文件wicd-curses.py.有一个名为wrap_exceptions的函数,然后在文件的其他几个地方,我发现了一些像@wrap_exceptions这样的代码,它发生在其他几个函数之前.这是什么意思 ?