如果我想使用键而不是按钮我应该如何以及在何处绑定它们? 蒂姆帮助我解决了调整大小问题,但我的简单代码变得相当复杂。我正在玩游戏并且已经与python一起出现了。我的目标是使用按钮废弃并绑定一些键来启动/停止和闪烁 我应该为这些功能创建一个新的类还是主要的? 另外,如果我想在开始时全屏显示最好将overideredirect弹出到我的重绘类中?
import tkinter
import time
class ReDraw():
def __init__(self):
self._tk = tkinter.Tk()
self._myframe = tkinter.Frame(master = self._tk)
self._myframe.grid(row = 0, column = 0, sticky = tkinter.NSEW)
self._mycanvas = tkinter.Canvas(master = self._tk, width = 850, height = 400, bg = 'grey', highlightthickness = 0)
self._mycanvas.grid(row = 0, column = 0, sticky = tkinter.NSEW)
self._mycanvas.bind("<Configure>", self._on_resize)
self._tk.rowconfigure(0, weight = 1)
self._tk.columnconfigure(0, weight = 1)
self.fracX = 1
self.fracY = 1
self._oval1 = self._mycanvas.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="black", tags = 'oval1')
self._oval2 = self._mycanvas.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="black", tags = 'oval2')
self._oval3 = self._mycanvas.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="black", tags = 'oval3')
self._oval4 = self._mycanvas.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="black", tags = 'oval4')
self._oval5 = self._mycanvas.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="black", tags = 'oval5')
def _on_resize(self, event):
self._mycanvas.delete(tkinter.ALL)
self.fracX = event.width/850
self.fracY = event.height/400
self._oval1 = self._mycanvas.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="black", tags = 'oval1')
self._oval2 = self._mycanvas.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="black", tags = 'oval2')
self._oval3 = self._mycanvas.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="black", tags = 'oval3')
self._oval4 = self._mycanvas.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="black", tags = 'oval4')
self._oval5 = self._mycanvas.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="black", tags = 'oval5')
def main():
canvas = ReDraw()
# Some widgets to the canvas
def red0(a):
for i in range(a):
canvas._mycanvas.delete('oval1')
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval1')
canvas._tk.update()
time.sleep(0.05)
def red1(a):
for i in range(a):
canvas._mycanvas.delete('oval1')
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval1')
canvas._mycanvas.delete('oval2')
canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracX,50*canvas.fracY,320*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval2')
canvas._tk.update()
time.sleep(0.05)
def red2(a):
for i in range(a):
canvas._mycanvas.delete('oval1')
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval1')
canvas._mycanvas.delete('oval2')
canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracX,50*canvas.fracY,320*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval2')
canvas._mycanvas.delete('oval3')
canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracX,50*canvas.fracY,480*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval3')
canvas._tk.update()
time.sleep(0.05)
def red3(a):
for i in range(a):
canvas._mycanvas.delete('oval1')
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval1')
canvas._mycanvas.delete('oval2')
canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracX,50*canvas.fracY,320*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval2')
canvas._mycanvas.delete('oval3')
canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracX,50*canvas.fracY,480*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval3')
canvas._mycanvas.delete('oval4')
canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracX,50*canvas.fracY,640*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval4')
canvas._tk.update()
time.sleep(0.05)
def red4(a):
for i in range(a):
canvas._mycanvas.delete('oval5')
canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracX,50*canvas.fracY,800*canvas.fracX,160*canvas.fracY, fill="red", tags = 'oval5')
canvas._tk.update()
time.sleep(0.05)
def green(a):
for i in range(a):
canvas._mycanvas.delete(tkinter.ALL)
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="green", tags = 'oval1')
canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracX,50*canvas.fracY,320*canvas.fracX,160*canvas.fracY, fill="green", tags = 'oval2')
canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracX,50*canvas.fracY,480*canvas.fracX,160*canvas.fracY, fill="green", tags = 'oval3')
canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracX,50*canvas.fracY,640*canvas.fracX,160*canvas.fracY, fill="green", tags = 'oval4')
canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracX,50*canvas.fracY,800*canvas.fracX,160*canvas.fracY, fill="green", tags = 'oval5')
canvas._tk.update()
time.sleep(0.05)
def lights():
canvas._mycanvas.delete(tkinter.ALL)
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval1')
canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracX,50*canvas.fracY,320*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval2')
canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracX,50*canvas.fracY,480*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval3')
canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracX,50*canvas.fracY,640*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval4')
canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracX,50*canvas.fracY,800*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval5')
canvas._tk.update()
def lights1(a):
for i in range(a):
canvas._mycanvas.delete(tkinter.ALL)
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval1')
canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracX,50*canvas.fracY,320*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval2')
canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracX,50*canvas.fracY,480*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval3')
canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracX,50*canvas.fracY,640*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval4')
canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracX,50*canvas.fracY,800*canvas.fracX,160*canvas.fracY, fill="black", tags = 'oval5')
canvas._tk.update()
time.sleep(0.05)
def amber(a):
for i in range(a):
canvas._mycanvas.delete(tkinter.ALL)
canvas._oval1 = canvas._mycanvas.create_oval(50*canvas.fracX,50*canvas.fracY,160*canvas.fracX,160*canvas.fracY, fill="yellow", tags = 'oval1')
canvas._oval2 = canvas._mycanvas.create_oval(210*canvas.fracX,50*canvas.fracY,320*canvas.fracX,160*canvas.fracY, fill="yellow", tags = 'oval2')
canvas._oval3 = canvas._mycanvas.create_oval(370*canvas.fracX,50*canvas.fracY,480*canvas.fracX,160*canvas.fracY, fill="yellow", tags = 'oval3')
canvas._oval4 = canvas._mycanvas.create_oval(530*canvas.fracX,50*canvas.fracY,640*canvas.fracX,160*canvas.fracY, fill="yellow", tags = 'oval4')
canvas._oval5 = canvas._mycanvas.create_oval(690*canvas.fracX,50*canvas.fracY,800*canvas.fracX,160*canvas.fracY, fill="yellow", tags = 'oval5')
canvas._tk.update()
time.sleep(0.05)
# tag all of the drawn widgets
def flashing():
while True:
amber(12)
lights1(10)
def stop():
while True:
canvas._mycanvas.delete(tkinter.ALL)
lights()
canvas._tk.update()
def start():
while True:
canvas._mycanvas.delete(tkinter.ALL)
lights()
canvas._tk.update()
lights1(20)
red0(35)
red1(35)
red2(35)
red3(35)
red4(35)
green(90)
lights()
break
canvas._but1 = tkinter.Button(text="start", command=start)
canvas._but1.grid(row = 2, column = 0, sticky="w")
canvas._but2 = tkinter.Button(text="amber", command=flashing)
canvas._but2.grid(row = 2, sticky="w", padx=(80))
canvas._but1 = tkinter.Button(text="stop", command=stop)
canvas._but1.grid(row = 2, column = 0, sticky="w", padx=(40))
if __name__ == "__main__":
main()
作为一个快速的例子来理解这样的事情......
import tkinter
class ReDraw():
def __init__(self):
self._tk = tkinter.Tk()
self._myframe = tkinter.Frame(master = self._tk)
self._myframe.grid(row = 0, column = 0, sticky = tkinter.NSEW)
class main(tk.Frame):
def main(tk.Frame):
canvas._but1 = tkinter.Button(text="start", command=start)
canvas._but1.grid(row = 2, column = 0, sticky="w")
class MyCanvas(tkinter.Canvas):
def MyCanvas(tkinter.Canvas)
canvas = ReDraw()
# Some widgets to the canvas
def red0(a):
答案 0 :(得分:0)
您可以使用Main(tk.Frame)
和tkinter.Tk
创建课程Frame
,使用画布创建所有功能的第二课MyCanvas(tkinter.Canvas)
。
修改强>
它仍然不是我想的,但至少它起作用。
import Tkinter as tk
import time
class App():
def __init__(self):
self._tk = tk.Tk()
self._tk.rowconfigure(0, weight = 1)
self._tk.columnconfigure(0, weight = 1)
self._myframe = tk.Frame(master = self._tk)
self._myframe.grid(row = 0, column = 0, sticky = tk.NSEW)
self._mycanvas = MyCanvas(self._tk)
self._tk._but1 = tk.Button(self._tk, text="start", command=self._mycanvas.start)
self._tk._but1.grid(row = 2, column = 0, sticky="w")
self._tk._but2 = tk.Button(self._tk, text="amber", command=self._mycanvas.flashing)
self._tk._but2.grid(row = 2, sticky="w", padx=(80))
self._tk._but1 = tk.Button(self._tk, text="stop", command=self._mycanvas.stop)
self._tk._but1.grid(row = 2, column = 0, sticky="w", padx=(40))
self._tk.bind("s", self._mycanvas.start)
self._tk.bind("w", self._mycanvas.stop)
self._tk.mainloop()
class MyCanvas():
def __init__(self, parent):
self.parent = parent
self._myself = tk.Canvas(master = parent, width = 850, height = 400, bg = 'grey', highlightthickness = 0)
self._myself.grid(row = 0, column = 0, sticky = tk.NSEW)
self.fracX = 1
self.fracY = 1
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="black", tags = 'oval1')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="black", tags = 'oval2')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="black", tags = 'oval3')
self._oval4 = self._myself.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="black", tags = 'oval4')
self._oval5 = self._myself.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="black", tags = 'oval5')
self._started = False
self._myself.bind("<Configure>", self._on_resize)
def _on_resize(self, event):
self._myself.delete(tk.ALL)
self.fracX = event.width/850
self.fracY = event.height/400
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="black", tags = 'oval1')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="black", tags = 'oval2')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="black", tags = 'oval3')
self._oval4 = self._myself.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="black", tags = 'oval4')
self._oval5 = self._myself.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="black", tags = 'oval5')
# Some widgets to the canvas
def red0(self, a):
for i in range(a):
self._myself.delete('oval1')
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="red", tags = 'oval1')
self.parent.update()
time.sleep(0.05)
def red1(self, a):
for i in range(a):
self._myself.delete('oval1')
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="red", tags = 'oval1')
self._myself.delete('oval2')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="red", tags = 'oval2')
self.parent.update()
time.sleep(0.05)
def red2(self, a):
for i in range(a):
self._myself.delete('oval1')
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="red", tags = 'oval1')
self._myself.delete('oval2')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="red", tags = 'oval2')
self._myself.delete('oval3')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="red", tags = 'oval3')
self.parent.update()
time.sleep(0.05)
def red3(self, a):
for i in range(a):
self._myself.delete('oval1')
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="red", tags = 'oval1')
self._myself.delete('oval2')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="red", tags = 'oval2')
self._myself.delete('oval3')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="red", tags = 'oval3')
self._myself.delete('oval4')
self._oval4 = self._myself.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="red", tags = 'oval4')
self.parent.update()
time.sleep(0.05)
def red4(self, a):
for i in range(a):
self._myself.delete('oval5')
self._oval5 = self._myself.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="red", tags = 'oval5')
self.parent.update()
time.sleep(0.05)
def green(self, a):
for i in range(a):
self._myself.delete(tk.ALL)
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="green", tags = 'oval1')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="green", tags = 'oval2')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="green", tags = 'oval3')
self._oval4 = self._myself.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="green", tags = 'oval4')
self._oval5 = self._myself.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="green", tags = 'oval5')
self.parent.update()
time.sleep(0.05)
def lights(self):
self._myself.delete(tk.ALL)
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="black", tags = 'oval1')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="black", tags = 'oval2')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="black", tags = 'oval3')
self._oval4 = self._myself.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="black", tags = 'oval4')
self._oval5 = self._myself.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="black", tags = 'oval5')
self.parent.update()
def lights1(self, a):
for i in range(a):
self._myself.delete(tk.ALL)
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="black", tags = 'oval1')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="black", tags = 'oval2')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="black", tags = 'oval3')
self._oval4 = self._myself.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="black", tags = 'oval4')
self._oval5 = self._myself.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="black", tags = 'oval5')
self.parent.update()
time.sleep(0.05)
def amber(self, a):
for i in range(a):
self._myself.delete(tk.ALL)
self._oval1 = self._myself.create_oval(50*self.fracX,50*self.fracY,160*self.fracX,160*self.fracY, fill="yellow", tags = 'oval1')
self._oval2 = self._myself.create_oval(210*self.fracX,50*self.fracY,320*self.fracX,160*self.fracY, fill="yellow", tags = 'oval2')
self._oval3 = self._myself.create_oval(370*self.fracX,50*self.fracY,480*self.fracX,160*self.fracY, fill="yellow", tags = 'oval3')
self._oval4 = self._myself.create_oval(530*self.fracX,50*self.fracY,640*self.fracX,160*self.fracY, fill="yellow", tags = 'oval4')
self._oval5 = self._myself.create_oval(690*self.fracX,50*self.fracY,800*self.fracX,160*self.fracY, fill="yellow", tags = 'oval5')
self.parent.update()
time.sleep(0.05)
# tag all of the drawn widgets
def flashing(self):
if not self._started:
self.amber(12)
self.lights1(10)
self._myself.after(100, self.flashing)
def stop(self, extra=None):
if self._started:
self._started = False
self._myself.delete(tk.ALL)
self.parent.update()
def start(self, extra=None):
if not self._started:
self._started = True
self._myself.delete(tk.ALL)
self.lights()
self.lights1(20)
self.red0(35)
self.red1(35)
self.red2(35)
self.red3(35)
self.red4(35)
self.green(90)
self.lights()
#----------------------------------------------------------------------
if __name__ == "__main__":
App()
class MyCanvas():
应为class MyCanvas(tk>Canvas):
,然后您需要tk.Canvas.__init__(...)
中的MyCanvas.__init__(...)
,而您不需要self._myself = tk.Canvas(...)
,self
已经画布(代替self._myself
)。