动态添加项目到 Tkinter Canvas

发布于 2024-09-01 04:26:59 字数 1317 浏览 9 评论 0原文

我正在尝试学习 Tkinter,目的是能够创建一个“实时”范围来绘制数据。作为测试,我尝试在每次按下“绘制”按钮时在画布上绘制多边形。三角形的位置是随机的。我有两个问题:

  1. 程序一启动,画布上就会出现一个三角形,为什么以及如何解决这个问题?
  2. 当我按下按钮时,它不会绘制任何三角形,至少我看不到。

代码:

from Tkinter import *

from random import randint

class App:
    
    def __init__(self,master):
        
        #frame = Frame(master)
        #frame.pack(side = LEFT)
        
        self.plotspc = Canvas(master,height = 100, width = 200, bg = "white")
        self.plotspc.grid(row=0,column = 2, rowspan = 5)
        
        self.button = Button(master, text = "Quit", fg = "red", \
                             command = master.quit)
        self.button.grid(row=0,column=0)
        
        self.drawbutton = Button(master, text = "Draw", command = \
                               self.pt([50,50]))
        self.drawbutton.grid(row = 0, column = 1)        
        
    def pt(self, coords):
        coords[0] = coords[0] + randint(-20,20)
        coords[1] = coords[1] + randint(-20,20)
        x = (0,5,10)
        y = (0,10,0)
        xp = [coords[0] + xv for xv in x]
        yp = [coords[1] + yv for yv in y]
        ptf = zip(xp,yp)
        self.plotspc.create_polygon(*ptf)        

    if __name__ == "__main__":
        root = Tk()
        app = App(root)
        root.mainloop()

I'm attempting to learn Tkinter with the goal of being able to create a 'real-time' scope to plot data. As a test, I'm trying to draw a polygon on the canvas every time the 'draw' button is pressed. The triangle position is randomized. I have two problems:

  1. There is a triangle on the canvas as soon as the program starts, why and how do I fix this?
  2. It doesn't draw any triangles when I press the button, at least none that I can see.

Code:

from Tkinter import *

from random import randint

class App:
    
    def __init__(self,master):
        
        #frame = Frame(master)
        #frame.pack(side = LEFT)
        
        self.plotspc = Canvas(master,height = 100, width = 200, bg = "white")
        self.plotspc.grid(row=0,column = 2, rowspan = 5)
        
        self.button = Button(master, text = "Quit", fg = "red", \
                             command = master.quit)
        self.button.grid(row=0,column=0)
        
        self.drawbutton = Button(master, text = "Draw", command = \
                               self.pt([50,50]))
        self.drawbutton.grid(row = 0, column = 1)        
        
    def pt(self, coords):
        coords[0] = coords[0] + randint(-20,20)
        coords[1] = coords[1] + randint(-20,20)
        x = (0,5,10)
        y = (0,10,0)
        xp = [coords[0] + xv for xv in x]
        yp = [coords[1] + yv for yv in y]
        ptf = zip(xp,yp)
        self.plotspc.create_polygon(*ptf)        

    if __name__ == "__main__":
        root = Tk()
        app = App(root)
        root.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2024-09-08 04:26:59

command=self.pt([50,50])(您在构建绘制按钮的 Button 调用中使用)立即执行调用您告诉它执行的内容,并将结果 (None) 绑定到 command。相反,使用相同的方法:

, command=lambda: self.pt([50, 50]) )

将调用的执行延迟到每次按下该按钮时。

command=self.pt([50,50]) (that you use in the Button call which builds the Draw button) immediately executes the call you're telling it to execute, and binds the result (None) to command. Use, instead, in that same:

, command=lambda: self.pt([50, 50]) )

to delay the execution of the call to each time that button will be pressed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文