Cosinus drawing

2019-02-27 22:03发布

I'd like to make a program which draws a cosinus graph in orderd range. But there is an error which I'm not able to repair. Error Message: "z = int(self.entry.get()) AttributeError: program instance has no attribute 'entry" Here is my code:

 # -*- 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()

2条回答
来,给爷笑一个
2楼-- · 2019-02-27 22:44

Replace:

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)

to

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)

Otherwise, at line z = int(self.entry.get()), self.entry would not exist. Also, the grid method doesn't return anything so, if you do everything in one line as you did, you lose your Entry object and affect None to entry.

查看更多
狗以群分
3楼-- · 2019-02-27 22:53

When making the variables, you'll have to set them as instance variables:

    self.entry = Entry(self, justify = 'center').grid(row = 2,column = 0,columnspan = 2 ,sticky = E+ W)
    self.entry1 = Entry(self, justify = 'center').grid(row = 2,column = 4,columnspan = 2, sticky = E)
登录 后发表回答