[英]How can I use 'getch' as input for moving an object in 'tkinter'?
I am trying to make a replica of dodger using python. 我正在尝试使用python制作道奇的副本。 In my code, I made a window using tkinter
. 在我的代码中,我使用tkinter
制作了一个窗口。 I tried to use getch()
as a way of inputting arrow key values to make the main character move. 我尝试使用getch()
作为输入箭头键值以使主角色移动的方式。 Here is my code: 这是我的代码:
from tkinter import *
from msvcrt import getch
import time
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.initWindow()
def initWindow(self):
self.master.title('Dodger')
self.pack(fill=BOTH, expand=1)
self.master.geometry('600x800')
self.master.config(bg='black')
menu = Menu(self.master)
self.master.config(menu=menu)
def clientExit():
exit()
file = Menu(menu)
file.add_command(label='Exit', command=clientExit)
file.add_command(label='Start', command=self.game)
menu.add_cascade(label='File', menu=file)
def game(self):
canvas = Canvas(self.master, width='600', height='800')
canvas.pack()
canvas.create_rectangle(0, 0, 600, 800, fill='black', outline='black')
character = canvas.create_rectangle(270, 730, 330, 760, fill='blue', outline='red')
left = 75
right = 77
time.sleep(10)
while True:
if ord(getch()) == left:
canvas.move(character, -5, 0)
canvas.update()
elif ord(getch()) == right:
canvas.move(character, 5, 0)
canvas.update()
root = Tk()
app = Window(root)
app.mainloop()
As you can see, in the def game(self)
function, I created a rectangle as the character. 如您所见,在def game(self)
函数中,我创建了一个矩形作为角色。 Then I used getch()
to compare keyboard inputs; 然后,我使用getch()
比较键盘输入; pressing the right/left arrow key will move the character respectively. 按左右箭头键将分别移动字符。 However, this does not work in the window: my window freezes and it says "not responding". 但是,这在窗口中不起作用:我的窗口冻结,并显示“未响应”。 I am forced to close the window, thus I do not know if my code is not working or if my computer sucks. 我被迫关闭窗口,因此我不知道我的代码是否无法正常工作或计算机是否运行异常。 Copy and paste this into your editor and please let me know how I can fix this, if possible. 将其复制并粘贴到您的编辑器中,如果可以的话,请告诉我如何解决。
BTW, when you load the window, click file, then start. 顺便说一句,当您加载窗口时,单击文件,然后启动。
I have modified your code to follow PEP8 a little better and corrected the game()
method by removing the sleep()
method and adding 2 more methods
for controlling left and right movement. 我修改了您的代码以更好地遵循PEP8,并通过删除sleep()
方法并添加了另外methods
控制左右移动的methods
来更正了game()
方法。
By making sure our Canvas is a class attribute and the character is a class attribute we can interact with them from any method within the class. 通过确保Canvas是类属性,而角色是类属性,我们可以从类中的任何方法与它们进行交互。
I made everything into a class attribute that I thought should probably be one. 我把所有东西都变成了我认为应该是的类属性。
Updated to include max left and max right. 更新为包括最大左侧和最大右侧。
import tkinter as tk
class Window(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master.title('Dodger')
self.master.geometry('600x800')
self.master.config(bg='black')
menu = tk.Menu(self.master)
file = tk.Menu(menu)
file.add_command(label='Exit', command=exit)
file.add_command(label='Start', command=self.game)
menu.add_cascade(label='File', menu=file)
self.master.config(menu=menu)
self.canvas = None
self.character = None
self.master.bind("<Left>", self.left_key)
self.master.bind("<Right>", self.right_key)
def game(self):
self.canvas = tk.Canvas(self.master, width='600', height='800')
self.canvas.pack()
self.canvas.create_rectangle(0, 0, 600, 800, fill='black', outline='black')
self.character = self.canvas.create_rectangle(270, 730, 330, 760, fill='blue', outline='red')
def left_key(self, event):
cords = self.canvas.coords(self.character)
if cords[0] <= 5:
print("Max left")
else:
self.canvas.move(self.character, -5, 0)
def right_key(self, event):
cords = self.canvas.coords(self.character)
if cords[2] >= 595:
print("Max Right")
else:
self.canvas.move(self.character, 5, 0)
root = tk.Tk()
app = Window(root).pack(fill="both", expand=1)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.