我想知道我是如何总是把窗口放在前景的。如果我点击另一个区域,窗口将处于“非活动”状态,我将无法再访问它(这不是真正的问题)。例如,当我打开浏览器或游戏时,我希望它处于前景,即使我单击其他曲面时也是如此。”-“最顶端”已可用,该窗口仅在其他活动中消失。
我发布代码是为了更好地了解我的意思(但我自己认为如果有一个解决方案的话,将会有一个小的解决方案)
from tkinter import *
from tkinter import ttk
class MainWindow(Tk):
def __init__(self):
super(MainWindow, self).__init__()
self.style = ttk.Style()
self.style.theme_use("clam")
self.style.configure("TButton", background="red", borderwidth=0, focuscolor="none")
self.style.configure("TONE.TButton", background="#1e2224", font=("Unispace", 12), borderwidth=1,
foreground="#a1e0ff", bordercolor="lime", darkcolor="lime", lightcolor="lightgreen")
self.bgFrame = []
for x in range(5):
frm = Frame(self, bg="red")
frm.pack(fill="x", ipady=30)
self.bgFrame.append(frm)
Button(self.bgFrame[0], text="\u26cc", bg="red", bd=0, command=self.destroy).pack(side="right", anchor="ne")
self.firstRow = []
x1 = 0
width1 = [80, 80, 80, 80, 80, 80]
for x in range(6):
firstRow = ttk.Button(self.bgFrame[0])
firstRow.place(x=x1, y=0, width=width1[x], height=60)
self.firstRow.append(firstRow)
x1 += 80
self.secondRow = []
x2 = [0, 100, 180, 260, 340, 420, 500]
width2 = [100, 80, 80, 80, 80, 80]
for x in range(6):
secondRow = ttk.Button(self.bgFrame[1])
secondRow.place(x=x2[x], y=0, width=width2[x], height=60)
self.secondRow.append(secondRow)
x3 = [0, 120, 200, 280, 360, 440, 520]
width3 = [120, 80, 80, 80, 80, 80]
self.thirdRow = []
for x in range(6):
thirdRow = ttk.Button(self.bgFrame[2])
thirdRow.place(x=x3[x], y=0, width=width3[x], height=60)
self.thirdRow.append(thirdRow)
self.fourthRow = []
x4 = [0, 100, 180, 260, 340, 420, 500]
width4 = [100, 80, 80, 80, 80, 80]
for x in range(6):
fourthRow = ttk.Button(self.bgFrame[3])
fourthRow.place(x=x4[x], y=0, width=width4[x], height=60)
self.fourthRow.append(fourthRow)
x5 = 0
width5 = [100, 100, 100, 300]
self.fithRow = []
for x in range(4):
fithRow = ttk.Button(self.bgFrame[4])
fithRow.place(x=x5, y=0, width=width5[x], height=60)
self.fithRow.append(fithRow)
x5 += 100
self.bind("<Key>", self.keyBind)
self.bind("<KeyRelease>", self.keyRelease)
def keyBind(self, event):
for i, num in enumerate(range(5), 1):
if event.char == str(i).upper() or event.char == str(i).lower():
self.firstRow[i].config(text=event.char.upper(), style="TONE.TButton")
for i, txt in enumerate(["q", "w", "e", "r", "t"], 1):
if event.char == str(txt).upper() or event.char == str(txt).lower():
self.secondRow[i].config(text=event.char.upper(), style="TONE.TButton")
for i, txt in enumerate(["a", "s", "d", "f", "g"], 1):
if event.char == str(txt).upper() or event.char == str(txt).lower():
self.thirdRow[i].config(text=event.char.upper(), style="TONE.TButton")
for i, txt in enumerate(["<", "y", "x", "c", "v"], 1):
if event.char == str(txt).upper() or event.char == str(txt).lower():
self.fourthRow[i].config(text=event.char.upper(), style="TONE.TButton")
if event.keysym == "Tab":
self.secondRow[0].config(text="\u2B7E", style="TONE.TButton")
if event.keysym == "Shift_L":
self.fourthRow[0].config(text="\u21E7", style="TONE.TButton")
if event.keysym == "Control_L":
self.fithRow[0].config(text="STRG", style="TONE.TButton")
if event.keysym == "Alt_L":
self.fithRow[2].config(text="ALT", style="TONE.TButton")
if event.keysym == "space":
self.fithRow[3].config(text="SPACE", style="TONE.TButton")
def keyRelease(self, event):
for i in range(6):
self.firstRow[i].config(text="", style="TButton")
for i in range(6):
self.secondRow[i].config(text="", style="TButton")
for i in range(6):
self.thirdRow[i].config(text="", style="TButton")
for i in range(6):
self.fourthRow[i].config(text="", style="TButton")
for i in range(4):
self.fithRow[i].config(text="", style="TButton")
if __name__ == '__main__':
mw = MainWindow()
mw.geometry("700x340+0+0")
mw["bg"] = "red"
mw.overrideredirect(1)
mw.wm_transient()
mw.attributes("-transparentcolor", "red")
mw.attributes("-topmost", True)
mw.mainloop()
1条答案
按热度按时间1qczuiv01#
试着这样做:
我用了
keyboard.hook
挂接到所有系统范围的键盘事件。然后我使用了一个带有tkinter
循环来处理事件。