python - 余弦绘图

标签 python matplotlib draw

我想制作一个程序,在有序范围内绘制余弦图。但有一个错误我无法修复。错误消息:“z = int(self.entry.get()) AttributeError:程序实例没有属性“entry”这是我的代码:

 # -*- coding: utf-8 -*-

from Tkinter import Tk, W, E
from ttk import Label, Button, Frame, Entry,Style
import math
import sys
import matplotlib as mp

class program(Frame):

    def __init__(self,main):

        Frame.__init__(self,main)        
        self.main = main       
        self.initUI()

    def initUI(self):

        self.main.title('COSINUSEK')
        Style().configure('TFrame', background = 'black')
        Style().configure('TLabel', background = 'black', foreground = 'blue')
        Style().configure("TButton", background = 'red', foreground = 'blue')


        self.rowconfigure(0, pad = 3)
        self.rowconfigure(1, pad = 3)
        self.rowconfigure(2, pad = 3)
        self.rowconfigure(3, pad = 3)
        self.rowconfigure(4, pad = 3)

        self.columnconfigure(0,pad =3)
        self.columnconfigure(1,pad =3)
        self.columnconfigure(2,pad =3)
        self.columnconfigure(3,pad =3)
        self.columnconfigure(4,pad =3)



        label = Label(self, text = 'Podaj zakres w stopniach').grid(row = 0,column = 3)
        od = Label(self, text = '     OD').grid(row = 1, column =0)
        do = Label(self, text = '             DO').grid(row = 1, column =4 )
        entry = Entry(self, justify = 'center').grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
        entry1 = Entry(self, justify = 'center').grid(row = 2,column = 4,columnspan = 2, sticky = E)
        button =  Button(self, text = 'Ok',command = self.ok).grid(row = 3,column = 3)
        button1 = Button(self, text = 'Draw', command = self.dra).grid(row = 4, column = 3)

        self.pack()

    def run(self):
        self.main.mainloop()

    def ok(self):
        x = []
        y = []
        z = int(self.entry.get())
        w = int(self.entry1.get())
        i = w
        while i in range(w,z):
            x.append(i)
            for a in x:
                y[a] = math.cos((x[a]*math.pi)/180)
            i = i + 0.01
    def dra(self):
        self.mp.ion() 
        self.mp.plot(self.x,self.y)
        self.mp.title('Wykres')
        self.mp.xlabel('x')
        self.mp.ylabel('y')
        self.mp.draw()  



program(Tk()).run()

最佳答案

替换:

entry = Entry(self, justify = 'center').grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
entry1 = Entry(self, justify = 'center').grid(row = 2,column = 4,columnspan = 2, sticky = E)

self.entry = Entry(self, justify = 'center')
self.entry.grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
self.entry1 = Entry(self, justify = 'center')
self.entry1.grid(row = 2,column = 4,columnspan = 2, sticky = E)

否则,在 z = int(self.entry.get()) 行,self.entry 将不存在。此外,grid 方法不会返回任何内容,因此,如果您像以前那样在一行中完成所有操作,则会丢失 Entry 对象并影响 None条目

关于python - 余弦绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20030425/

相关文章:

python - 将包含元组列表的字典转换为列表

python - matplotlib 中有没有办法检查当前显示的轴区域中有哪些艺术家?

swift - 绘制带有约束的形状 swift 4

python - 根据另一列中的条件填充多个数据框列

python - 无法在 PyCharm 中加载 DLL python 模块。在 IPython 中运行良好

python - matplotlib pyplot 2 在同一图中使用不同轴绘制

python - Matplotlib:3D trisurf 图中的 ax.format_coord() - 返回 (x,y,z) 而不是(方位角,仰角)?

python - 具有随机像素颜色的 100x100 图像

html - 如何在悬停时在html中绘制 Logo

python - httplib 与 urllib2 和 cookies