在下面的代碼中,我試圖讓兩個不同的矩形分別改變它們的顏色,每次我釋放第一個矩形的鍵“d”和第二個矩形的鍵“f”(這是因為否則用戶可以自由地按住兩個鍵之一并毫無困難地增加點擊次數),但我不知道如何系結任何特定鍵的 KeyRelease。從 tkinter 匯入 * 匯入時間
def round_rectangle(canvas ,x1, y1, x2, y2, radius=25, **kwargs):
points = [x1 radius, y1,
x1 radius, y1,
x2-radius, y1,
x2-radius, y1,
x2, y1,
x2, y1 radius,
x2, y1 radius,
x2, y2-radius,
x2, y2-radius,
x2, y2,
x2-radius, y2,
x2-radius, y2,
x1 radius, y2,
x1 radius, y2,
x1, y2,
x1, y2-radius,
x1, y2-radius,
x1, y1 radius,
x1, y1 radius,
x1, y1]
return canvas.create_polygon(points, **kwargs, smooth=True)
def click_one(event):
global Clicks
MainCanvas.itemconfig(FirstButton, fill="#ff0000")
window.update()
time.sleep(0.05)
MainCanvas.itemconfig(FirstButton, fill="#111111")
Clicks = 1
ClicksVar.set(Clicks)
def click_two(event):
global Clicks
MainCanvas.itemconfig(SecondButton, fill="#ff0000")
window.update()
time.sleep(0.05)
MainCanvas.itemconfig(SecondButton, fill="#111111")
Clicks = 1
ClicksVar.set(Clicks)
window = Tk()
window.geometry("960x600")
window.config(bg="#000000")
Clicks = 0
ClicksVar = IntVar()
window.bind("<KeyRelease>", click_one)
window.bind("<KeyRelease>", click_two)
MainCanvas = Canvas(window, width=960, height=600, bg="#000000")
MainCanvas.pack()
FirstButton = round_rectangle(MainCanvas ,385, 255, 475, 345, fill="#111111")
SecondButton = round_rectangle(MainCanvas ,485, 255, 575, 345, fill="#111111")
ClickLabel = Label(MainCanvas, font=("Premium", 15), width=3, height=1, textvariable=ClicksVar, bg="#111111", fg="#777777")
ClickLabel.place(x=462, y=350)
window.mainloop()
uj5u.com熱心網友回復:
那將是您問題的答案。如果您的廣告是對 keydown 和 keyup-function 的 if 陳述句,則您有一個特定鍵的系結。
def keydown(e):
print(e.char)
def keyup(e):
print(e.char)
widget.bind("<KeyPress>", keydown)
widget.bind("<KeyRelease>", keyup)
uj5u.com熱心網友回復:
如何在 tkinter 中系結任何特定密鑰的 KeyRelease?
您可以與 一起指定特定鍵KeyRelease。例如,以下代碼click_one在釋放“d”鍵和釋放click_two“f”鍵時呼叫。
window.bind("<KeyRelease-d>", click_one)
window.bind("<KeyRelease-f>", click_two)
有關事件說明符的權威檔案,請參閱tcl/tk 手冊頁中的事件模式。
uj5u.com熱心網友回復:
當我想系結一個鍵來做一個動作時,我會使用它。您可以嘗試以下方法:
import keyboard
shortcut = "F1"
if keyboard.is_pressed(shortcut):
<yourcode>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/462086.html
標籤:Python tkinter tkinter-canvas
