我在學習,慢慢地!以下代碼的一部分來自這裏關於在tkinter畫布中調整對象大小的問題。無論如何,我的目標是使開始的網格燈,因爲我瞭解更多,我將增加啓動和停止按鈕或鍵的能力,但現在我只是想讓它在視覺上工作。 我希望它適合完美的任何屏幕我運行它沒有改變代碼等,但是當我重新調整窗口的大小,它開始在不同的地方添加圈子,它讓我非常困惑。調整大小會導致錯誤?
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 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)
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 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()
# tag all of the drawn widgets
def flashing():
global flag
flag=1
while flag:
if flag == 1:
lights1(9)
amber(10)
lights()
def stop():
global flag
flag = 2
while flag:
if flag == 2:
lights()
def start():
global flag
flag = 3
while flag:
if flag == 3:
lights()
red0(30)
red1(30)
red2(30)
red3(30)
red4(30)
green(30)
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()
再重新編輯整理的大部分問題,但仍然認爲這是錯誤
當您調整大小時停止繪圖。 – furas
嗨,請你解釋一下。謝謝。 –