随着时间的推移自动形状移动

时间:2018-10-17 20:33:51

标签: python python-3.x tkinter

当我运行当前程序时,当我按下按钮时,该程序上的三角形仅移动一次。我希望我的三角形形状每单位时间自动移动一个像素,但是我不确定如何使它工作。

到目前为止,在执行此程序时,我仍然无法弄清楚哪些类和定义在哪里以及如何使用这些类和定义来使它们像我上面打算的那样工作。

import tkinter as tk
from tkinter import PhotoImage
from tkinter import *
import time

counter = 0
running = False   

class triangle():
    def trianglemovedefine(move_x, move_y):
        canvas.move (triangle3, move_x, move_y)

    def trianglemove():
        triangle.trianglemovedefine(1, 0)

def moveitboi(justmoveit):
    def count(): 
        if running: 
            global counter 
            justmoveit = lambda:triangle.trianglemove()
            #My "per unit time" adjusted to per 1000 microseconds
            justmoveit.after(1000, count)  
            counter += 1
    count()

def Start(justmoveit):
    global running
    running=True
    moveitboi(justmoveit)

root = tk.Tk()
root.geometry("960x600")

label_toptitle = tk.Label(root, text="Program Name", font=(None, 40),)
label_toptitle.grid(row=0, columnspan=3)

label_desc = tk.Label(root, image=pixel, compound="center", width=900, font=(None, 14),
                                          padx=20, pady=10, text=description)

label_desc.grid(row=1, columnspan=3)

canvas = tk.Canvas(width=960, height=300, bg='white')
canvas.grid(row=2, column=0, columnspan=3)

for linecounter in range(49):
        newtextbit = linecounter + 1
        if (newtextbit + 3) % 4 == 0 and newtextbit != 49:
                canvas.create_text((linecounter * 16 + 80), 90,
                                           fill="darkblue",
                                           font="Times 10 bold",
                                           text=newtextbit)
        if (newtextbit + 3) % 4 == 0:
                canvas.create_line(((linecounter * 16 + 80)), 40, ((linecounter * 16 + 80)), 70,
                                           width=1,
                                           fill="black"
                                           )
        else:
                canvas.create_line(((linecounter * 16 + 80)), 50, ((linecounter * 16 + 80)), 70,
                                           width=1,
                                           fill="black"
                                           )
canvas.create_line(73, 70, 860, 70,
                                   width=2,
                                   fill="black"
                                   )
#The Triangle
triangle3 = canvas.create_polygon(75, 25, 86, 25, 80, 40, fill ='red')

f1 = tk.Frame(root, width=70, height=30)
f1.grid(row=3, column=0, sticky='W')

button_record = tk.Button(f1,
                          text="Record",
                          compound="top",
                          command=lambda:triangle.trianglemove(),
                          )

button_record.pack(side='left', padx=140)


root.mainloop()

1 个答案:

答案 0 :(得分:2)

在对问题代码进行了相当广泛的更改之后,最终我得到了可以运行的东西,足以说明如何实现我在评论中发布的关于如何使用通用after()的建议。小部件方法。

请注意,moveitboi()的最后一行如何呼叫after(),以在指定的延迟后安排另一个对其自身的呼叫。

在进行更改时,我还尝试使代码大部分遵循PEP 8 - Style Guide for Python Code建议。我强烈建议您阅读并在以后编写自己的代码时进行同样的操作。

import tkinter as tk

class Triangle:
    def __init__(self, canvas, *points, fill=''):
        self.canvas = canvas
        self.obj_id = canvas.create_polygon(*points, fill=fill)

    def move(self, move_x, move_y):
        self.canvas.move(self.obj_id, move_x, move_y)

    def move_right(self):
        self.move(1, 0)


def moveitboi(parent, marker):
    global counter

    if running:
        marker.move_right()
        counter += 1

    # Call this func again after delay.
    parent.after(1000, moveitboi, parent, marker)

def start(parent, marker):
    global running

    if not running:
        running = True
        moveitboi(parent, marker)  # Start triangle position updates.


root = tk.Tk()
root.geometry("960x600")

counter = 0
running = False

label_toptitle = tk.Label(root, text="Program Name", font=(None, 40),)
label_toptitle.grid(row=0, columnspan=3)

# Left out because I don't have the "pixel" image.
#label_desc = tk.Label(root, image=pixel, compound=tk.CENTER, width=900, font=(None, 14),
#                      padx=20, pady=10, text=description)

canvas = tk.Canvas(root, width=960, height=300, bg='white')
canvas.grid(row=2, column=0, columnspan=3)

for linecounter in range(49):
    newtextbit = linecounter + 1
    xposn = linecounter*16 + 80

    if (newtextbit + 3) % 4 == 0 and newtextbit != 49:
        canvas.create_text(xposn, 90, fill="darkblue", font="Times 10 bold",
                           text=newtextbit)
    if (newtextbit + 3) % 4 == 0:
        canvas.create_line(xposn, 40, xposn, 70, width=1, fill="black")
    else:
        canvas.create_line(xposn, 50, xposn, 70, width=1, fill="black")  

canvas.create_line(73, 70, 860, 70, width=2, fill="black")

# Global triangle.
triangle3 = Triangle(canvas, 75, 25, 86, 25, 80, 40, fill='red')

f1 = tk.Frame(root, width=70, height=30)
f1.grid(row=3, column=0, sticky='W')

button_record = tk.Button(f1, text="Record", compound="top",
                          command=lambda: start(root, triangle3))
button_record.pack(side='left', padx=140)

root.mainloop()

下面的屏幕快照显示了单击 Record 按钮几秒钟后红色标记的移动位置:

screenshot after clicking Record button