【发布时间】:2020-07-09 01:41:55
【问题描述】:
我使用 tkinter 构建了一个带有 3 个按钮的简单 GUI 窗口,并使用 matplotlib 显示了一个简单的图形。过程是:按下第一个按钮启用第二个按钮。按第二个按钮显示一个数字,然后启用第三个按钮。按第三个按钮显示图形并保存图形。 但是,按下第二个按钮后,第三个按钮将无法启用。如果我关闭程序,则会发生错误。 另一个问题:为什么保存的图片是空的?谢谢。
import matplotlib.pyplot as plt
from tkinter import *
import numpy as np
class MyGUI:
def __init__(self):
Win=Tk()
Win.geometry('750x640')
Win.resizable(False,False)
Win.update()
w=Win.winfo_width()
h=Win.winfo_height()
Btn1=Button(Win,text='enable button 2',command=self.Btn1Listener)
Btn1.place(x=20,y=20)
Btn1.update()
self.Btn2=Button(Win,text='Plot 1',command=self.Btn2Listener)
self.Btn2.place(x=Btn1.winfo_x()+Btn1.winfo_width()+10,y=20,width=150)
self.Btn2.configure(state='disabled')
self.Btn2.update()
self.Btn3=Button(Win,text='plot 2',command=self.Btn3Listener)
self.Btn3.place(x=self.Btn2.winfo_x()+self.Btn2.winfo_width()+10,y=20,width=150)
self.Btn3.configure(state='disabled')
self.Btn3.update()
mainloop()
#-------------------------------------------------------
def Btn1Listener(self):
self.Btn2.configure(state='normal')
def Btn2Listener(self):
global v,s
v,s=self.B1()
self.Btn3.configure(state='normal')
def Btn3Listener(self):
self.B2(s,v)
#-------------------------------------------------------------
def B1(self):
x=np.arange(0,5,0.1)
y1=np.sin(x)
plt.plot(x,y1)
plt.show()
return 0,5
def B2(self, s,v):
x=np.arange(s,v,0.1)
y1=np.sin(x)
plt.plot(x,y1)
plt.show()
plt.savefig('aaa.png')
#------------------------------------------------------------------
if __name__ =='__main__':
MyApp=MyGUI()
【问题讨论】:
标签: tkinter