简体   繁体   English

Tkinter程序更新屏幕,但我不明白如何在没有canvas.create命令的情况下

[英]Tkinter program updates screen but I dont understand how without any canvas.create commands

In this code I modified from the Web, canvas.create statements are used to create the grid and the text as usual but there is no create statement in the function AddPoint which is called to update the plot so I don't see how the plot gets written. 在我从Web修改的这段代码中,canvas.create语句用于照常创建网格和文本,但是AddPoint函数中没有create语句,该函数被调用来更新绘图,因此我看不到绘图如何被写。 What statement causes the plot to update ? 什么语句导致剧情更新?

    import Tkinter,  time, sys, math
    from random import *
    global latch
    start = 50
    end =   800
    class HPSA:

        def HPSA_grid(self):                                                                               # create a grid
            self.lines=[]
            self.lastpos=0
            self.canvas.create_rectangle(0, 0, 800, 512, fill="black")
            self.canvas.create_text(25, 20, fill="red", text=str("dB"), anchor="e")
            self.canvas.create_text(770, 500, fill="red", text=str("MHz"), anchor="e")
            # horizontal lines   
            for y in range(0,480,50):                                                                      
                    self.canvas.create_line(0, y, 800, y, fill="#444444",dash=(4, 4))
                    #self.canvas.create_line(0, y, 800, y, fill="red",dash=(4, 4))
                    self.canvas.create_text(5, y-10, fill="#999999", text=str(-y*2/10), anchor="w")
            # vertical lines  
            for x in range(start,800,80):                                                                   
                    self.canvas.create_line(x, 0, x, 512, fill="#444444",dash=(4, 4))
                    #self.canvas.create_line(x, 0, x, 512, fill=red,dash=(4, 4))
                    self.canvas.create_text(x+3, 500-10, fill="#999999", text=str(x/10), anchor="w")
            # draw start vertical line        
            self.canvas.create_line(start, 0, start, 470, fill="yellow")   
            # draw start horizontal line                                
            self.canvas.create_line(start, 470, 800, 470, fill="yellow")                                  

            self.lineRedraw=self.canvas.create_line(0, 800, 0, 0, fill="red")

           # self.lines1text=self.canvas.create_text(800-3, 10, fill="#00FF00", text=str("TEST"), anchor="e")
            for x in range(800):
                    self.lines.append(self.canvas.create_line(x, 0, x, 0, fill="#00FF00"))

        def addPoint(self,val):
            if self.xpos>start:
                pass
                self.canvas.coords(self.lines[self.xpos],(self.xpos-1,self.lastpos,self.xpos,val))
            if self.xpos<800:
                pass
                #self.canvas.coords(self.lineRedraw,(self.xpos+1,val +5,self.xpos+1,val-5))           #  ?? draws red line near val
            self.lastpos=val
            self.xpos+=1
            if self.xpos==800:
                    self.xpos=0
                    self.totalPoints+=800
                    latch = 0
                    my_screen.update()       # continuosly draws points     
        def __init__(self, my_screen):
            self.xpos=start
            self.line1avg=0
            self.data=[0]*800
            self.canvas = Tkinter.Canvas(my_screen, width=800, height=512)
            self.canvas.pack()
            self.totalPoints=0
            self.HPSA_grid()
            self.timeStart=time.time()

    my_screen = Tkinter.Tk()
    my_screen.wm_title("HPSA")
    a = HPSA(my_screen)
    my_screen.update()    # draws screen immediately
    k= .041
    for x in range (10,800,1):
      point = 250 - 250*(math.sin(k*x)/(k*x))  
      a.addPoint(point)

It appears that this code creates 800 points as zero-length lines when it starts, and addPoint simply adjusts the coordinates of those lines in order to draw the graph. 看起来,这段代码在开始时会创建800个零长度线点,而addPoint只是调整这些线的坐标以绘制图形。

Here is the code that initially creates the 800 lines: 这是最初创建800行的代码:

for x in range(800):
    self.lines.append(self.canvas.create_line(x, 0, x, 0, fill="#00FF00"))

Inside addPoint , it modifies the coordinates of each of those lines here: addPoint内部,它在这里修改了这些行的坐标:

self.canvas.coords(self.lines[self.xpos],(self.xpos-1,self.lastpos,self.xpos,val))

You could get the same effect by removing the loop that initially creates all of the lines, and then replacing that line in addPos with a call to create_line . 你可以通过删除最初创建的所有行的循环,然后替换该行获得同样的效果addPos通过调用create_line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM