Tôi muốn biết cách đơn giản nhất để liên kết các khóa trong Python
Ví dụ: cửa sổ bảng điều khiển Python mặc định và chờ đợi, sau đó trong psuedo ->
if key "Y" is pressed:
print ["Yes"]
if key "N" is pressed:
print ["No"]
Tôi muốn đạt được điều này mà không cần sử dụng bất kỳ mô -đun nào không được bao gồm bởi Python. Chỉ là Python thuần túywithout the use of any modules not included by python. just pure python
Bất kỳ và tất cả sự giúp đỡ đều được đánh giá rất cao
Python 2.7 hoặc 3.x Windows 7
Lưu ý:
1 yêu cầu người dùng nhấn enter và do đó không liên quan class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
1 requires the user to hit enter and is therefore not keybindingĐã hỏi ngày 23 tháng 7 năm 2013 lúc 10:33Jul 23, 2013 at 10:33
DCA-DCA-DCA-
1.2222 Huy hiệu vàng18 Huy hiệu bạc33 Huy hiệu đồng2 gold badges18 silver badges33 bronze badges
1
Từ //code.activestate.com/recipes/134892/ [mặc dù một chút đơn giản]:
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
Sau đó, bạn có thể làm:
>>> getch[]
'Y' # Here I typed Y
Điều này là tuyệt vời vì nó không cần bất kỳ mô -đun bên thứ 3 nào.
Đã trả lời ngày 23 tháng 7 năm 2013 lúc 10:46Jul 23, 2013 at 10:46
TerryaterryaTerryA
57.4K11 Huy hiệu vàng117 Huy hiệu bạc140 Huy hiệu đồng11 gold badges117 silver badges140 bronze badges
5
Chà, cách để thực hiện với Tkinter, một mô -đun có trong cài đặt Python là ở đây:
from tkinter import *
window = Tk[]
window.geometry["600x400"]
window.title["Test"]
def test[event]:
print["Hi"]
window.bind["a", test]
window.mainloop[]
Đã trả lời ngày 12 tháng 1 năm 2017 lúc 18:25Jan 12, 2017 at 18:25
Nếu bạn có màn hình, bạn có thể thích điều này:
screen = turtle.Screen[]
def blabla:
# your code here
screen.listen[]
screen.onkey[blabla, "[any key here]"]
Divibisan
10,9k11 Huy hiệu vàng39 Huy hiệu bạc58 Huy hiệu đồng11 gold badges39 silver badges58 bronze badges
Đã trả lời ngày 30 tháng 5 năm 2018 lúc 18:20May 30, 2018 at 18:20
2
Liên kết chính trong Tkinter là một giá trị vẫn giúp bạn tạo ra các ứng dụng GUI phức tạp. Khái niệm này rất đơn giản. Bạn liên kết với một khóa cụ thể, hoặc loại khóa với các chức năng thực thi khi nhấn phím đó.
Hình thức
Mã dưới đây cho thấy cách sử dụng hầu hết các ràng buộc chính trong Tkinter. Chúng tôi có một sự cố nhỏ và giải thích mã ở đây trong hướng dẫn này, nhưng tôi khuyên bạn nên xem hướng dẫn video của chúng tôi cho các ràng buộc khóa Tkinter. Trong đó, chúng tôi tạo lại mã dưới đây từ đầu, giải thích từng bước một cách chi tiết trên đường đi.
import tkinter as tk class MainWindow: def __init__[self, master]: self.master = master self.usernames = ["Player1", "CodersLegacy", "Knight"] self.frame = tk.Frame[self.master, width = 300, height = 300] self.frame.pack[] self.label = tk.Label[self.frame, text = "This is some sample text"] self.label.place[ x = 80, y = 20] self.button = tk.Button[self.frame, text = "Button"] self.button.place[ x = 120, y = 80] self.entry = tk.Entry[self.frame] self.entry.place[ x = 80, y = 160] self.entry2 = tk.Entry[self.frame] self.entry2.place[ x = 80, y = 200] self.bindings[] def bindings[self]: self.master.bind['a', lambda event: print["A was pressed"]] self.frame.bind['', lambda event: print["Entered Frame"]] self.label.bind['', lambda event: print["Mouse clicked the label"]] self.button.bind['', lambda event: self.color_change[self.button, "green"]] self.button.bind['', lambda event: self.color_change[self.button, "black"]] self.entry.bind['', lambda event: self.pass_check[]] self.entry.bind['', lambda event: self.Focused_entry[]] self.entry.bind['', lambda event: self.UnFocused_entry[]] def color_change[self, widget, color]: widget.config[foreground = color] def pass_check[self]: text = self.entry.get[] for username in self.usernames: if text == username: print["Username taken"] def Focused_entry[self]: print["Focused [Entered] the entry widget"] def UnFocused_entry[self]: print["UnFocused [Left] the entry widget"]
Giải trình
Trong phần này, chúng tôi sẽ giải thích từng ràng buộc chính mà chúng tôi đã sử dụng trong mã ở trên.
Lưu ý: Lý do chúng tôi sử dụng lambdas, để chúng tôi có thể chuyển các tham số vào hàm được gọi. Hàm mà bạn chuyển vào tham số thứ hai của
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
2 không nên có bất kỳ dấu ngoặc nào. Do đó, Lambdas là một cách để làm việc xung quanh giới hạn này. [Vấn đề này phát sinh vì bạn cần một tên hàm trong class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
2, không phải là một cuộc gọi chức năng.self.master.bind['a', lambda event: print["A was pressed"]]
Mã trên sẽ in ra, A A đã được nhấn vào màn hình, nếu nhấn phím của A A. Chúng ta có thể ràng buộc bất kỳ khóa nào theo cách này, chẳng hạn như B B ,, C C ,, 1 1, v.v.
self.frame.bind['', lambda event: print["Entered Frame"]]
Trong mã trên, chúng tôi in ra khung nhập khung, nếu con trỏ chuột di chuyển qua khung. Hãy thử chạy mã để xem đúng.
self.label.bind['', lambda event: print["Mouse clicked the label"]]
Mã trên sẽ khiến Tkinter in ra một tin nhắn, nếu chúng ta nhấp vào nhãn với chuột.
self.button.bind['',lambda event: self.color_change[self.button,"green"]] self.button.bind['',lambda event: self.color_change[self.button,"black"]]
Mã trên tạo hiệu ứng di chuột. Nếu bạn nhập nút Nút bằng con trỏ của bạn, thì nó sẽ đổi màu sang màu xanh lá cây. Nếu rời khỏi nút, nó sẽ thay đổi màu trở lại màu đen.
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
0Mã này có hai mục đích. Các ràng buộc, tiếp tục kiểm tra xem tên người dùng có tồn tại không. Mỗi lần bạn nhập một khóa mới, nó sẽ kiểm tra tên người dùng đã nhập với danh sách các tên người dùng hiện có bằng cách gọi hàm
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
6.Hai ràng buộc khác kiểm tra xem tiện ích nhập có tập trung hay không. Nếu bạn nhấp vào tiện ích nhập, chức năng
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
7 sẽ gọi. Nếu bạn chọn một số tiện ích khác như một mục nhập khác, thì nó sẽ trở nên mất tập trung và gọi hàm class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__[self]:
self.impl = _GetchUnix[]
def __call__[self]:
return self.impl[]
class _GetchUnix:
def __init__[self]:
import tty, sys
def __call__[self]:
import sys, tty, termios
fd = sys.stdin.fileno[]
old_settings = termios.tcgetattr[fd]
try:
tty.setraw[sys.stdin.fileno[]]
ch = sys.stdin.read[1]
finally:
termios.tcsetattr[fd, termios.TCSADRAIN, old_settings]
return ch
getch = _Getch[]
8.Danh sách các ràng buộc chính
Dưới đây là danh sách các ràng buộc chính trong Tkinter. Hãy nhớ rằng, bạn có thể gắn chúng vào bất kỳ tiện ích nào, không chỉ là cửa sổ Tkinter.
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Đối với nhấp chuột bên trái, cho chuột giữa [bánh xe cuộn], để nhấp chuột phải. | |
Được sử dụng để giữ một phím chuột trong khi kéo nó. để giữ và kéo nút chuột trái, v.v. | Được sử dụng để phát hiện khi nút chuột được giải phóng. |
Được sử dụng để phát hiện nhấp chuột của chuột.
Các tiện ích đã được nhập vào, như con trỏ chuột bay lơ lửng trên nó.
Tiện ích này đã được rời khỏi bên trái, chẳng hạn như con trỏ chuột rời khỏi khu vực chiếm đóng của nó.