Python图形用户界面和动画

2024-10-02 18:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个图形用户界面,用tkinter绘制温度传感器的实时图形,并从matplotlib中创建动画函数。我得到了漂亮的现场图表。但我面临的问题是,运行应用程序后,实时图形就开始运行了。我已经创建了一个按钮来调用类StartPage中的命令,但它不起作用。我希望我的应用程序在我单击class StartPage中的开始监视时开始收集数据和绘制图形

如果有人能帮我这个忙,那就太好了。在

谢谢你。在

def animate(i):

ipcon = IPConnection() # Create IP connection
ptc1 = BrickletPTC(UID1, ipcon) # S3 
ptc2 = BrickletPTC(UID2, ipcon) # S7
ptc3 = BrickletPTC(UID3, ipcon) # S7
ptc4 = BrickletPTC(UID4, ipcon) # S7

ipcon.connect(HOST, PORT) # Connect to brickd
temperature1 = ptc1.get_temperature() - 20
temperature2 = ptc2.get_temperature() + 6
temperature3 = ptc3.get_temperature() + 3
temperature4 = ptc4.get_temperature() + 29

l = [temperature1, temperature2]
avgTemp = np.mean(l)
avgTempM = str(avgTemp/100)

dataArrayavg = avgTempM.split(',')
tempavg = float(dataArrayavg[0])
tempCavg.append(tempavg)
avg.set_xlabel('Time [s]', fontsize = 10)
avg.set_ylabel('Temperature [°C]', fontsize = 10)
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
avg.yaxis.set_major_formatter(y_formatter)
titleavg = "Average Room Temperature: " + str(temperature1/100) + " °C"
avg.set_title(titleavg, fontsize = 12)
avg.plot(tempCavg, "#483D8B")


f1.savefig('RoomTemperature.png')
f2.savefig('ObjectTemperature.png')


class StartPage(tk.Frame):


def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)

    # Adding a logo to the startpage
    logo = Image.open("Gemeinschaftslogo(5).jpg")
    Image1 = ImageTk.PhotoImage(logo)

    # defining the image label
    imageLabel = Label(self, image = Image1)
    imageLabel.image = Image1
    imageLabel.pack(pady=30, padx=30)

    #defining labels
    label = ttk.Label(self, text="Zustandsüberwachung (Condition Monitoring) von industriellen Computertomographie-Systemen", font = LARGE_FONT,)
    label.pack(pady=10, padx=10)

    #creating button
    #if agree then move to temperature graph
    button1 = ttk.Button(self, text="Start Monitoring", 
                        command=lambda: animate())  # with lambda we are moving from start page to page 1

    button1.pack(pady=30, padx=30)

    button2 = ttk.Button(self, text="Enter", 
                        command=lambda: controller.show_frame(RoomTempGraph))  # with lambda we are moving from start page to page 1

ani1 = animation.FuncAnimation(f1,animate, interval = 1000)
app.mainloop()

Tags: tolambdaself图形getpageavgtemperature
3条回答

只是想办法解决我自己的问题。我希望它能帮助某人

以前应用程序主循环()我创建了一个测试函数

app.geometry("1280x720")


def test():

   ani1 = animation.FuncAnimation(f1,animate1, interval = 1000)
   ani2 = animation.FuncAnimation(f2,animate2, interval = 1000)
   ani1._start(test)
   ani2._start(test)

app.mainloop()

我把它传给了页面上的命令按钮

^{pr2}$

我不知道它是怎么工作的,但当我要求它时,它就开始密谋了。。:天

再次感谢大家。 祝你今天愉快

与Jean-François Fabre的解决方案类似,这不会阻止animate()的运行,但会阻止它执行任何操作。在

def animate(i):
   global draw
   if draw:
      """ here goes all you want your animate func to do """
   draw = False

所以,通过添加你想做什么,如果画你将确保它将只画一次,然后不再画。如果你想让它继续被绘制,最后删除draw=False。根据需要,需要设置draw ininit。在

^{pr2}$

这至少是我如何在我的程序中实现这种功能的。显然,你需要一种方法在某个地方将draw设置为真,而这可能是任何情况。在我的程序中,我把它设置为每当我改变要绘制的内容时绘制,但是你可能需要一个按钮,因为你希望它继续运行。在

好吧,肮脏的黑客:

global do_animate
do_animate = False

更改命令如下:

^{pr2}$

animate中返回if do_animate == False

相关问题 更多 >