在tkinter窗口打开时使用控制台

2024-05-14 09:45:08 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我尝试在透明窗口运行时使用控制台。 所以应该有一个控制台和一个tkinter窗口。 两者都应该可用。 我尝试了多线程和多处理,但都不起作用。 我还尝试在一个额外的文件中创建tkinter窗口,并使用os.system(winow.py)运行它,但是对于没有安装python的人来说,这是行不通的

from threading import Thread
import tkinter
import os
import keyboard
import time

FONT = "{Verdana} 10 bold"

btest = True

txt = """
    Text here
"""
root = tkinter.Tk()

def overlay():
    text = tkinter.Text(root,relief=tkinter.FLAT, 
                font=FONT,state='normal', height=5, bg='grey70', fg='blue')
    text.insert(tkinter.END, txt)
    text.config(state=tkinter.DISABLED)     #make it readonly with calling state=diabled    ,state=normal is write
    text.pack(expand=tkinter.YES, fill=tkinter.BOTH)

    button = tkinter.Button(root, text='Exit', bd=1, command=quit)
    button.pack(side=tkinter.BOTTOM, fill=tkinter.X)

    text.insert(tkinter.END, txt)

    root.geometry('200x250+1710-820')
    root.overrideredirect(1) # fenster ohne aussen rum :-)

    root.attributes('-alpha', 0.75) # fenster transparent
    root.attributes('-topmost', 1) # fenster immer im vordergrund
    root.mainloop()

def printgui():
    os.system("cls")
    print(f"Test: {btest} [alt]")

Thread(target=printgui()).start()
Thread(target=overlay()).start()

while True:
    if(keyboard.is_pressed("alt")):         #if u press "alt" it will change true to False
        btest = not btest
        time.sleep(0.2)
        printgui()

    if(keyboard.is_pressed("delete")):
        print("\n")
        print("Exiting")
        time.sleep(3)
        os._exit(1)

Tags: textimporttxttimeisostkinterroot

热门问题