我正在尝试使用stdlib为我的PyQt5应用程序构建一个python shell,InteractiveConsole这样我就可以让用户编写实时脚本了.我正在使用a QTextEdit来显示shell中的stdout.
当我在外壳循环做,应用程序冻结,因为insertPlainText()对QTextEdit太快.所以我写了一个缓冲区,可以将插入延迟几毫秒.但是,我注意到,只要我time.sleep()在for循环中运行任何阻塞函数,它就会冻结.所以for循环中的打印只会在循环完成后显示.如果禁用缓冲区,则不会发生这种情况.
例如,如果我在shell中执行此操作:
>>>for i in range(10):
... time.sleep(1)
... print(i)
...
Run Code Online (Sandbox Code Playgroud)
这只会在10秒后打印.
这是我根据MVCE指南编写的最小版本.
这是main.ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>main_window</class>
<widget class="QMainWindow" name="main_window">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<widget class="QWidget" name="central_widget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="console_layout">
<item>
<widget …Run Code Online (Sandbox Code Playgroud) 我有一个这样的文件夹结构:
- modules
- root
- abc
hello.py
__init__.py
- xyz
hi.py
__init__.py
blah.py
__init__.py
foo.py
bar.py
__init_.py
Run Code Online (Sandbox Code Playgroud)
这是字符串格式的相同内容:
"modules",
"modues/__init__.py",
"modules/foo.py",
"modules/bar.py",
"modules/root",
"modules/root/__init__.py",
"modules/root/blah,py",
"modules/root/abc",
"modules/root/abc/__init__.py",
"modules/root/abc/hello.py",
"modules/root/xyz",
"modules/root/xyz/__init__.py",
"modules/root/xyz/hi.py"
Run Code Online (Sandbox Code Playgroud)
我正在尝试以 python 导入样式格式打印出所有模块。一个示例输出是这样的:
modules.foo
modules.bar
modules.root.blah
modules.root.abc.hello
modules.root.xyz.hi
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 python 中轻松地做到这一点(如果可能的话,没有第三方库)?
- modules
- root
- abc
hello.py
__init__.py
- xyz
hi.py
__init__.py
blah.py
__init__.py
foo.py
bar.py
__init_.py
Run Code Online (Sandbox Code Playgroud)
但是,此代码只会打印出 'foo' 和 'bar'。但不是“root”,它是子包。我也无法弄清楚如何转换它以保留它的绝对导入样式。当前代码只获取包/模块名称,而不是实际的绝对导入。