- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
import Tkinter as tk
import os
from hhh import hello
def runshell():
root.destroy()
hello()
root=tk.Tk()
nvar=tk.StringVar(root)
en=tk.Entry(textvariable=nvar)
en.pack()
btn=tk.Button(text="Shell", command=runshell)
btn.pack()
root.mainloop()
这是上面的 Tkinter GUI 代码。
import time
import sys
import ctypes
ctypes.windll.kernel32.SetConsoleTitleA("HELLO WORLD")
def hello():
def printf(s):
for c in s:
sys.stdout.write('%s' % c)
sys.stdout.flush()
time.sleep(0.15)
printf('Hello, World!')
上面的代码被命名为“hhh.py”,我在第一个代码中将其作为模块导入,并且需要在 CUI 中运行。我是windows平台。现在,如何隐藏启动 Tkinter 应用程序时弹出的控制台窗口,同时可以通过按按钮查看“hhh.py”的输出来重新打开它?请帮忙...!!!
最佳答案
隐藏现有的控制台窗口通常不是一个好主意。它是一个共享资源,如果您的应用程序因窗口隐藏而终止,则基本上会使连接到控制台的所有其他应用程序变得无用。
您可以通过 pythonw.exe 运行脚本,它不会自动分配或附加到控制台。然后按需分配自己的控制台,切换到全屏模式(如果支持),设置窗口标题,并将 sys.std*
重新绑定(bind)到控制台设备文件“CONIN$”和“CONOUT$” ”。您拥有此窗口的唯一所有权,因此您有权隐藏它。
例如:
import os
import sys
import time
import ctypes
import platform
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
from ctypes import wintypes
user32 = ctypes.WinDLL('user32', use_last_error=True)
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
_windows_version = tuple(map(int, platform.version().split('.')))
kernel32.GetConsoleWindow.restype = wintypes.HWND
user32.SendMessageW.argtypes = (wintypes.HWND, wintypes.UINT,
wintypes.WPARAM, wintypes.LPARAM)
user32.ShowWindow.argtypes = (wintypes.HWND, ctypes.c_int)
SW_HIDE = 0
SW_MAXIMIZE = 3
SW_SHOW = 5
WM_SYSKEYDOWN = 0x0104
VK_RETURN = 0x0D
def toggle_fullscreen(hwnd=None):
if _windows_version < (10, 0, 14393):
return
if hwnd is None:
hwnd = kernel32.GetConsoleWindow()
lparm = (user32.MapVirtualKeyW(VK_RETURN, 0) << 16) | 0x20000001
user32.SendMessageW(hwnd, WM_SYSKEYDOWN, VK_RETURN, lparm)
def printf(s):
for c in s:
sys.stdout.write('%s' % c)
sys.stdout.flush()
time.sleep(0.15)
def input(s):
sys.stdout.write(s)
sys.stdout.flush()
return sys.stdin.readline().rstrip('\n')
def hello():
kernel32.SetConsoleTitleW(u"Hello, World!")
printf('Hello, World!')
input('\nPress enter to continue...')
class App(object):
allocated_console = None
def __init__(self):
if self.allocated_console is None:
# one-time set up for all instances
allocated = bool(kernel32.AllocConsole())
App.allocated_console = allocated
if allocated:
hwnd = kernel32.GetConsoleWindow()
user32.ShowWindow(hwnd, SW_HIDE)
toggle_fullscreen(hwnd)
self.root = root = tk.Tk()
nvar = tk.StringVar(root)
en = tk.Entry(textvariable=nvar)
en.pack()
btn = tk.Button(text="Shell", command=self.runshell)
btn.pack()
def mainloop(self):
self.root.mainloop()
def runshell(self):
hwnd = kernel32.GetConsoleWindow()
user32.ShowWindow(hwnd, SW_SHOW)
try:
old_title = ctypes.create_unicode_buffer(512)
n = kernel32.GetConsoleTitleW(old_title, 512)
if n > 512:
old_title = ctypes.create_unicode_buffer(n)
kernel32.GetConsoleTitleW(old_title, n)
old_stdin = sys.stdin
old_stderr = sys.stderr
old_stdout = sys.stdout
try:
with open('CONIN$', 'r') as sys.stdin,\
open('CONOUT$', 'w') as sys.stdout,\
open('CONOUT$', 'w', buffering=1) as sys.stderr:
self.root.destroy()
hello()
finally:
kernel32.SetConsoleTitleW(old_title)
sys.stderr = old_stderr
sys.stdout = old_stdout
sys.stdin = old_stdin
finally:
if self.allocated_console:
user32.ShowWindow(hwnd, SW_HIDE)
if __name__ == '__main__':
for i in range(3):
app = App()
app.mainloop()
pythonw.exe 通常与 .pyw 文件扩展名相关联。您还可以配置 py2exe 等工具来创建非控制台可执行文件。
<小时/>我必须编写一个 input
函数,因为 raw_input
将其提示写入 stderr
FILE
流。我宁愿避免从 Python 重新绑定(bind) C 标准 I/O。
它通过使用 WM_SYSKEYDOW
消息将组合键 Alt+Enter 发送到控制台窗口来切换 Windows 10 中已分配控制台的全屏模式。 Windows Vista 到 Windows 8 不支持全屏模式。在这种情况下,您可以最大化窗口并调整屏幕缓冲区的大小。
请注意,我只是隐藏分配的控制台窗口。避免调用 FreeConsole
。 C 运行时的 conio API(例如 kbhit
、getch
)缓存“CONIN$”的句柄,但它不提供动态导出和支持的方法来重置此缓存句柄。这些 CRT 功能并非旨在支持在多个控制台上循环。假设一个进程在其生命周期内最多附加到一个控制台。至少在 Windows 10 中,此缓存句柄还可以防止未使用的控制台主机进程销毁其窗口并退出,直到您的进程退出。
如果用户在附加应用程序时关闭控制台,控制台将终止该应用程序。这是无法阻止的。您最多可以设置一个控制处理程序来通知进程即将被终止。
检查是否可以隐藏控制台窗口的另一种方法是调用 GetConsoleProcessList 来获取附加进程的列表。如果您的进程是唯一的,您有权隐藏该窗口。如果连接了两个进程,并且另一个是 Python 3 安装的 py[w].exe 启动器,那么隐藏窗口似乎是合理的。检查后者需要通过 OpenProcess
打开进程的句柄,以通过 GetModuleBaseName
获取图像名称。
关于python - 如何在启动 Tkinter 应用程序时隐藏控制台窗口,但在按下 GUI 按钮运行 python 脚本时重新打开它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43304505/
我需要检查在我的 RCP 应用程序中启动时是否加载了某些包。我知道有一个“主机 OSGi 控制台”可以显示 Eclipse IDE 中所有插件的状态,但我对这些不感兴趣。 我执行了以下步骤来获取我的应
在 pdb/ipdb 调试中,有用的 interact 命令为我提供了一个功能齐全的交互式 Python 控制台。 但是,这似乎始终是“标准”Python 控制台,即使我使用 ipdb 开始也是如此。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我搜索过但找不到答案:如何在运行 Emacs 时选择:文件、编辑、选项、缓冲区、工具、C++ 等下拉菜单在控制台模式下?不是终端菜单。 不,F10 不是答案。 最佳答案 如果不是 F10,那么 M-x
我正在制作一个每 20-40 秒截屏一次的 c# 控制台应用程序。 我试过到处找,但所有其他示例都没有使用控制台 这是我到目前为止所做的代码: using System; using System.D
尝试使用 terraform 控制台,新功能。 我使用 tfstate 进入我的项目并运行“terraform 控制台”。 我可以使用常规插值系统获取变量值、数据和资源。但是,模块很难破解,我无法正确
我正在尝试调试一段返回错误的 SQL。我不确定 django 或 mysql 是否处理错误,所以我想通过 django 控制台运行它。 有办法设置吗? 提前致谢。 最佳答案 manage.py dbs
你好是否可以在 JPanel 中绘制 java 控制台返回的内容?你有教程可以遵循吗?谢谢开关 最佳答案 我不记得在哪里找到这个,但我已使用我称为 TextAreaOutputStream 的类将输出
我对 Xcode 甚至编程都有点陌生。 在 Xcode 中,在我的代码中,如何显示控制台并清除屏幕? 我知道我可以使用 Xcode 首选项来完成此操作,但我想以编程方式完成此操作。 最佳答案 这对我有
我正在开发一个 C# 项目,我需要从没有 API 或 Web 服务的安全网站获取数据。我的计划是登录,访问我需要的页面,并解析 HTML 以获取记录到数据库所需的数据位。现在我正在使用控制台应用程序进
我是编程新手,正在尝试不同的在线事件以掌握它。我遇到了一个特定的问题,我想制作一个程序,用户输入一个值并打印一个特定的字符串。例如,当用户输入 0 时,将打印字符串“black”,输入 1 将打印字符
我想创建一个终端/控制台,用户可以在其中输入命令。我知道 java,但我是 xml 的新手,所以我想知道如何在文本下生成文本,如果它变得很长,它应该是可滚动的,这是一张图片: 这是我的 xml cpd
我有一个由随机生成的数字组成的 nxn 网格。我有一个标签显示 X 轴和 Y 轴的元素编号: 对于单个数字,它可以正确对齐,但是当网格大小增加时,标签会变得不成比例并且不会像这样对齐: 我想知道是否有
假设我创建了一个包含两个变量的结构。 struct mystruct{ public: string name; int age;}; class School :public mystruct{ p
我正在重写一个服务器程序,我想在其中添加一个简单的控制台输入。 目前,它只是提供数据并为它所做的每一件事打印出一两行,作为任何观看/调试的人的描述性措施。 我想要的是有一个始终位于底部的“粘性”输入栏
我必须编写启动另一个进程(GUI)的控制台应用程序。然后,使用其他应用程序或相同的选项,我必须能够停止子进程。此外,如果子进程从 GUI 关闭,则必须通知我执行最终任务(如果被杀死,则相同)。 我认为
我一直在尝试到处寻找以下问题的答案: Linux上的标准输出/控制台默认将内容保存到文件中吗? 我不想保存内容或重定向输出(我已经知道这一点),我只是想知道它是否已经通过 linux 中包含的某个默认
我正在尝试不同的事件,因为我是初学者并且想了解更多。我正在尝试在我的代码所在的同一行打印一个图案: int main() { int numOfWiggles; int count;
在我的一项小任务中,我被要求创建一个数组来存储从用户提供的输入中获取的姓名和地址,并且稍后能够从数组中删除姓名和地址。 如果能帮助我理解如何实现这一目标,我们将不胜感激,谢谢。 编辑 - 该数组将像地
如果您想在 Python shell 中查看特定模块中定义了哪些模块,一种选择是键入 dir(path.to.module)。不幸的是,这不仅列出了特定模块中定义的类或函数,还包括该模块导入的类或函数
我是一名优秀的程序员,十分优秀!