调整大小会导致错误?

时间:2014-06-27 17:51:46

标签: python python-2.7 python-3.x tkinter

我慢慢学习!以下代码的一部分来自这里关于调整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()

重新编辑对大多数问题进行了排序,但仍然认为它仍然很混乱仍然会出现大量错误

1 个答案:

答案 0 :(得分:-1)

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()

    # tag all of the drawn widgets
    for i in range(20):
        lights()
        red0(30)
        red1(30)
        red2(30)
        red3(30)
        red4(30)
        green(30)
        lights()

if __name__ == "__main__":
    main()

所以我只修复你的代码,它现在有效,只需检查一下!即使您在 main() ResizingCanvas (工作正常)中的画布大小,也会出现代码问题> 功能,您仍然会在特定位置的调整大小的画布中继续创建新的椭圆。

此外,我认为将整个tkinter应用程序创建为对象会更好,因为您可以更轻松地控制对象并创建方法或属性来获取数据。因此,在我的固定代码中,我将整个tkinter.Tk()对象放入我的 ReDraw()类中。然后,我在该类中创建椭圆并为每个类添加标签,因此我可以在 main() 功能中轻松删除我想要的椭圆。只需查看代码并询问我是否不理解它!

P / S:我希望您使用.grid()代替.pack(),因为它是调整窗口大小的更好工具,也更容易安排界面!