`
import matplotlib.pyplot as plt
import pandas as pd
l1 = [1,2,3,4]
l2 = [2,4,6,8]
fig = plt.figure()
def func():
plt.pause(1)
plt.plot(l1,l2)
plt.draw()
plt.pause(1)
input("press any key to continue...")
plt.close(fig)
plt.pause(1)
while True:
func()
plt.pause(1)
这是修改后的版本:
这是修改后的版本:
import matplotlib.pyplot as plt
import pandas as pd
l1 = [1,2,3,4]
l2 = [2,4,6,8]
fig = plt.figure()
a = 1
def func(num):
input(f"the {num}th window is not opened yet")
plt.pause(1)
plt.plot(l1,l2)
plt.draw()
print(f"the {num}th window is opened")
plt.pause(1)
input("press any key to continue...")
plt.close(fig)
plt.pause(1)
print(f"the {num}th window is closed")
while True:
func(a)
plt.pause(1)
a+=1
如果我不使用while True循环,只要我按下任何键,它就会停止运行,这正是我的意图。然而,如果我使用while True循环运行此代码,即使我按下任何键或左上角的x按钮,图形窗口也不会关闭。我认为这是由于while True引起的。我不知道如何解决这个问题,同时保持while True。请帮帮我!
- 修改: 当“第二个窗口尚未打开”输入消息出现时,我可以看到一个打开的窗口。可能,这个窗口是循环的第一次,因为第二个窗口在那时还没有打开。为什么第一个窗口还在呢?我使用plt.close()来关闭窗口。