gpt4 book ai didi

python - 如何在启动 Tkinter 应用程序时隐藏控制台窗口,但在按下 GUI 按钮运行 python 脚本时重新打开它?

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:38 25 4
gpt4 key购买 nike

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(例如 kbhitgetch)缓存“CONIN$”的句柄,但它不提供动态导出和支持的方法来重置此缓存句柄。这些 CRT 功能并非旨在支持在多个控制台上循环。假设一个进程在其生命周期内最多附加到一个控制台。至少在 Windows 10 中,此缓存句柄还可以防止未使用的控制台主机进程销毁其窗口并退出,直到您的进程退出。

如果用户在附加应用程序时关闭控制台,控制台将终止该应用程序。这是无法阻止的。您最多可以设置一个控制处理程序来通知进程即将被终止。

检查是否可以隐藏控制台窗口的另一种方法是调用 GetConsoleProcessList 来获取附加进程的列表。如果您的进程是唯一的,您有权隐藏该窗口。如果连接了两个进程,并且另一个是 Python 3 安装的 py[w].exe 启动器,那么隐藏窗口似乎是合理的。检查后者需要通过 OpenProcess 打开进程的句柄,以通过 GetModuleBaseName 获取图像名称。

关于python - 如何在启动 Tkinter 应用程序时隐藏控制台窗口,但在按下 GUI 按钮运行 python 脚本时重新打开它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43304505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com