Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用tkinter随时间自动移动形状_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 使用tkinter随时间自动移动形状

Python 使用tkinter随时间自动移动形状,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,当我运行当前程序时,当我按下按钮时,该程序上的三角形形状只移动一次。我希望我的三角形形状每单位时间自动移动一个像素,但我不知道如何使它工作 到目前为止,在做这个程序的时候,我仍然无法弄清楚类和定义的哪些部分去了哪里,以及如何处理这些类和定义,以使它们像上面所写的那样工作 import tkinter as tk from tkinter import PhotoImage from tkinter import * import time counter = 0 running = False

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

到目前为止,在做这个程序的时候,我仍然无法弄清楚类和定义的哪些部分去了哪里,以及如何处理这些类和定义,以使它们像上面所写的那样工作

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

在对您的问题中的代码进行了相当广泛的更改之后,最终我能够得到一些足够可运行的东西,以说明如何实现我在评论中提出的关于如何使用universal widget方法实现您想要的功能的建议

请注意,moveitboi的最后一行如何在指定的延迟后调用,以安排对自身的另一个调用

在进行更改的同时,我还试图使代码主要遵循建议。我强烈建议您阅读它,以后编写自己的代码时也这样做

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()
以下是一个屏幕截图,显示了在单击录制按钮几秒钟后红色标记的移动位置:


这类事情通常是通过使用通用小部件方法来完成的,该方法允许在指定的延迟后对显示进行更新调度。这里有很多tkinter的答案,说明了如何做到这一点,所以我建议你做更多的研究。回答有关如何制作时钟或制作动画的问题通常是很好的选择。我有after小部件,但我的问题是如何让after小部件按我的预期工作。到目前为止,按下按钮只能手动按下三角形一次。我试图等待程序再次移动三角形一秒钟,但我没有看到任何移动。@martineau我试图寻找其中一些类型的问题,但找不到任何问题。我应该查看任何推荐的URL链接吗?您的moveitboi函数只在一次之后调用。我们需要一遍又一遍地打电话让事情继续下去。给我一分钟,我会尝试为您找到一个好的…好的,但我目前不知道如何在小部件之后一遍又一遍地进行函数调用。我请求一些代码解决方案。感谢您提供指向PEP 8样式指南的链接。我想PEP8风格指南是一套编写高效Python代码的指南,不是吗?罗恩:很高兴听到你发现我的答案很有用。PEP8与其说是关于效率,不如说是关于编写可读的代码,这样可以更容易地维护,也可以让包括您在内的其他人稍后理解它在做什么。我不严格遵守政治公众人物第八章,只是大部分。用你自己的判断,它只是一些非常聪明的人的指导方针,它甚至说了一些类似的话。